GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Aug-2007, 12:49
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

App wont stay open


can anyone tell me why my proggy will not stay open,

CPP / C++ / C Code:
#include <iostream>

int main () {
    
    int question5;
    
    std::cout <<"Would you like to erase your harddrive [Y/N]: ";
    std::cin >> question5;
    switch (question5) {
           case 'Y':
           case 'y':
                std::cout <<"Fool you have lost everthing";
                std::cout <<"----------------------------------------------------\n";
           
           break;
           
           case 'N':
           case 'n':
                std::cout <<"That was the wrong answer you fool";
                std::cout <<"----------------------------------------------------\n";
                
           break;
           
           default:
                   std::cout <<"You did not type a valid input you fool";
                   std::cout <<"----------------------------------------------------\n";
                   
           break;
           }
               
               
    std::cout <<"Press anything to exti";
    std::cin.get();
    
    return 0;
}

thanks
  #2  
Old 02-Aug-2007, 14:23
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: App wont stay open


You can't be serious! This is probably the third, at least the second, thread where you ask the EXACTLY same thing. Unless you start these out just for the fun of it, check out your previous threads for some answers.
  #3  
Old 02-Aug-2007, 14:30
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: App wont stay open


ive tried what you previously suggested and ive added it in this code and what your suggested is a load of old coblers, try and complie it.
  #4  
Old 02-Aug-2007, 14:33
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: App wont stay open


All I have been suggesting is to use
CPP / C++ / C Code:
while (cin.get() != '\n') {}
Now where in your code do I see that?

EDIT

I don't mean to be rude, but I haven't seen you actually getting back to threads you have started. There's no sign that you actually have taken heed of the advice given or tested out the stuff shown in the previous threads. The picture I'm slowly getting is that you start threads, maybe look at the responses, maybe test out the code and see it works, but then when you encounter basically exactly the same situation in another context, you don't want to go back to the old threads to look for advice. Instead you start a new one.

If there's something wrong with the advice people give here, tell them so! If it just doesn't work, tell so! Walking away without a word and starting new threads really doesn't help anyone, since you're basically getting the same responses to the same problems over and over again.
Last edited by Kimmo : 02-Aug-2007 at 15:25.
  #5  
Old 02-Aug-2007, 16:00
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: App wont stay open


Quote:
Originally Posted by doom1992
can anyone tell me why my proggy will not stay open,

There are actually two problems:

CPP / C++ / C Code:
    
    int question5;
    
    std::cout <<"Would you like to erase your harddrive [Y/N]: ";
    std::cin >> question5;
Now if the user entere y,or Y, or n, or N or anything else that can not be converted to a decimal integer, cin goes into a "fail" state. Nothing else can be done with cin until it gets reset. (I won't bother you with details.) So any further attempts to use cin will fail (like when you call cin.get()). That means it will return without waiting for any further input since it can't do anything.

However, when you fix that (by declaring char question5; instead of int question5; you still have the same action: It doesn't wait before closing.


Here is why:

When a C++ program gets something from stdin (with cin >> or with cin.get(), for example) here is what happens:

As the user presses keys, the characters are stored in a system buffer (somewhere outside your program's data space) until the user presses 'Enter'.

Nothing is available to any program functions until the user presses 'Enter'. After the user presses 'Enter', the characters are available to the program input functions.

All of the charactes are now in the system buffer and a newline character ('\n') is at the end.

Consider the following:

CPP / C++ / C Code:
    char question5;
    
    std::cout <<"Would you like to erase your harddrive [Y/N]: ";
    std::cin >> question5;

Suppose the user presses the 'y' key and 'Enter'. So the buffer contains two chars: 'y' and '\n'

Now cin >> question5 removes the character 'y' from the systembuffer and sets question5 equal to this value. The '\n' is still in the system buffer.

Later, you do cin.get() and it retrieves the '\n' that was left in the buffer, and the program exits.

A quick fix (but not necessarily the best thing to do from the point of view of future requiremenes) is just to put another cin.get():

CPP / C++ / C Code:
#include <iostream>

int main () {
    
    char question5;
    
    std::cout <<"Would you like to erase your harddrive [Y/N]: ";
    std::cin >> question5;
    switch (question5) {
           case 'Y':
           case 'y':
                std::cout <<"Fool you have lost everthing";
                std::cout <<"----------------------------------------------------\n";
           
           break;
           
           case 'N':
           case 'n':
                std::cout <<"That was the wrong answer you fool";
                std::cout <<"----------------------------------------------------\n";
                
           break;
           
           default:
                   std::cout <<"You did not type a valid input you fool";
                   std::cout <<"----------------------------------------------------\n";
                   
           break;
           }
               
               
    std::cout <<"Press 'Enter' to exit...";
    std::cin.get();
    std::cin.get();
    
    return 0;
}



Regards,

Dave

Footnote: After testing the above program, let me know if it doesn't work as it should. Then and only then, go back and change question5 to an int, as it was in your original program. You will find that it will not wait for any more input on cin, no matter how many cin.get() statements you add. When cin is in the "fail" state, it can't do anything.
  #6  
Old 02-Aug-2007, 16:00
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: App wont stay open


thanks for the advice kimmo
  #7  
Old 02-Aug-2007, 16:06
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: App wont stay open


thanks dave, the best answer ive got so far, but i added in

CPP / C++ / C Code:
std::cin.ignore(100, '\n')

under which is ment to clear the buffer, but sill have the same problem, is there anyother way to clear the buffer. Thanks
  #8  
Old 02-Aug-2007, 16:08
doom1992 doom1992 is offline
Junior Member
 
Join Date: Jul 2007
Posts: 98
doom1992 is on a distinguished road

Re: App wont stay open


dosent matter i added this under in the case y and case n blocks and it worked. Thanks for your help
  #9  
Old 02-Aug-2007, 17:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: App wont stay open


Quote:
Originally Posted by doom1992
added in
CPP / C++ / C Code:
std::cin.ignore(100, '\n')


I like that much better, since it removes not only the next char, but lots more (In case the user entered "yes" instead of "y" for example.) I was just trying to keep it simple with my "quick fix".

Note that cin.ignore() only works if cin is not in the "fail" state, so a more complete solution, in general, might be

CPP / C++ / C Code:
    std::cout <<"Press 'Enter' to exit...";
    if (!std::cin) {
        std::cin.clear(); // clear the "fail" flag
    }
    std::cin.ignore(100 , '\n'); // note that clear() does not empty the buffer
    std::cin.get();
    


The clear() would not be needed if you had only used cin for chars. It might be necessary if you had numeric input failures (trying to read an int but the user entered alphabetic chars, for example).

Quote:
Originally Posted by doom1992
but still have the same problem
Are you saying that the program that I posted did not work? It works for me as long as the user enters exactly one character before 'Enter'. Using cin.ignore(100,'\n') followed by cin.get() instead of two cin.get() calls should also work.



Regards,

Dave
  #10  
Old 03-Aug-2007, 06:49
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: App wont stay open


I began playing around and ran into the following problem.

I want to display lines of text a certain amount of lines at a time. After that certain amount of lines shown, I of course want the user to have time to read the text. The user presses Enter when he wants the next lines shown.

For example, if I use the following:
CPP / C++ / C Code:
cout << "Press Enter to continue. . ." << endl;
cin.get();
Now this works if the user just presses the Enter. But if the user writes "Hi" and Enter, the program runs three loops without stopping.

Now if I use something like this:
CPP / C++ / C Code:
while (cin.get() != '\n') {}
cout << "Press Enter to continue. . ." << endl;
cin.get();
This, on the other hand, doesn't work if the user just presses Enter. Here user is required to press Enter twice.

I tried to think but couldn't come up with a nice solution. I can't use cin.peek() because it waits for input too, if there is none. I thought maybe there is some way of determining if there is input pending for the program. But didn't find any (or just didn't understand. ios_base can be hard to get for us newbies).

So what's the simple solution I just fail to see here?
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Console Will Not Stay Open doom1992 C++ Forum 15 16-Aug-2007 07:26
open google search from a vb program isnoend07 AdSense Forum 0 23-Jul-2007 17:48
Apache default page can't be visited satimis Apache Web Server Forum 0 05-Apr-2007 05:40
[ANN] New script engine: Open Basic (Basic syntax) MKTMK Computer Programming Advertisements & Offers 0 01-Sep-2005 07:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:09.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.