![]() |
|
#1
|
|||
|
|||
best way to pause program flow - cin.get?I am converting some C programs to C++ and I need to display some output and pause for the user to hit enter.
I have tried this with cin.get() and the only way I can get it to work is with two cin.get()'s. I have tried using << endl; and << flush; after the previous cout(). Can anyone clue me in on the best way to pause and wait for the enter key? I am using Linux g++. |
|
#2
|
|||
|
|||
Re: best way to pause program flow - cin.get?Quote:
There are ways and there are ways. I'm not sure that there is a "best" way. How did you do it in C? Regards, Dave |
|
#3
|
|||
|
|||
Re: best way to pause program flow - cin.get?The program had a loop that created a pause for a few seconds before returning to the main.
I know how to set up a yes / no branch. I just wanted to know if there is a function or a simple way to do this return on ENTER pause. Some examples I have seen use cin.get but it does not seem to work properly in linux. I have not tried it in Windows yet. Any ideas would be great. |
|
#4
|
||||
|
||||
Re: best way to pause program flow - cin.get?I'll bet you are at some point reading a character using cin. See this info about reading a character. scanf() is identical AFAIK to cin in the way they work on characters.
Now this is only a guess since you didn't post code. __________________
Age is unimportant -- except in cheese |
|
#5
|
||||
|
||||
Re: best way to pause program flow - cin.get?Hi,
You can find more information about pausing a program here. (written by WaltP ofcourse) Cheers, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#6
|
|||
|
|||
Re: best way to pause program flow - cin.get?I am sure the stream needs to be flushed because cin.get() is passed over on the first pass.
Here is the full code of a simple C program I wrote many years ago. I recently read the "Things not to do tutorial so I wanted to get rid of the scanf()'s. I replaced them with cin. 1 in main() and 1 in convert(). Some of the printf()'s are still there. I have tried cin.get() and cin.ignore(). I also tried << flush and << endl. This code compiles and runs fine on my linus box with 2 cin.get()'s. I am sure you can find all sorts of things wrong with it and any help would be great. CPP / C++ / C Code:
|
|
#7
|
||||
|
||||
Re: best way to pause program flow - cin.get?Quote:
Replacing scanf() with cin is like replacing a rotten apple with a rotten orange. They are both rotten. Essentially, cin is the C++ version of scanf(). Look into fgets() for C and cin.getline() for C++. flush and endl are only for output streams. Your problem is with the input stream. Looking at your code, you need to check out the formatting tutorial. You need to indent your code better. And use 4 spaces instead of TABs. TABs make the indents too deep IMHO. if pause_a_sec() pauses for 1 second as the name implies, why would you want to call it 1500 times to pause 5 seconds in pause_5_secs() Your case statements should only do the conversion and display the answer. Leave all the pausing and clearing until after the switch is finished. Also, there is no reason to clear the screen. It actually is annoying from the user's standpoint. I may want to look back on a previous answer, but you blanked the screen on me. In display_menu(int blank_line) you don't use the parameter. You should either use it or lose it from the parameter list. To the function convert() you pass an integer called choice. In the function itself, the parameter is magically changed to a float named quantity. But the quantity is entered in the function... Make the call and the definition the same -- use choice. Enough for now... That'll keep you busy. __________________
Age is unimportant -- except in cheese |
Recent GIDBlog
Halfway done! by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| US dollar amount, the result is all zeros. | Pinkyy | C Programming Language | 24 | 15-Sep-2005 17:28 |
| Type casts ? | kai85 | C++ Forum | 12 | 23-Jun-2005 12:04 |
| [TUTORIAL] Calling an external program in C (Linux) | dsmith | C Programming Language | 4 | 22-Apr-2005 13:30 |
| fltk-2.0 cvs | Plumb | FLTK Forum | 20 | 13-Nov-2004 07:10 |
| Need help with a C program (Long) | McFury | C Programming Language | 3 | 29-Apr-2004 20:06 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The