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 23-Oct-2005, 14:53
Jbonez Jbonez is offline
New Member
 
Join Date: Jul 2005
Posts: 7
Jbonez is on a distinguished road
Question

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  
Old 23-Oct-2005, 15:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,702
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: best way to pause program flow - cin.get?


Quote:
Originally Posted by Jbonez
I am converting some C programs to C++


I am using Linux g++.

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  
Old 23-Oct-2005, 16:10
Jbonez Jbonez is offline
New Member
 
Join Date: Jul 2005
Posts: 7
Jbonez is on a distinguished road

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  
Old 23-Oct-2005, 17:39
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

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  
Old 23-Oct-2005, 19:45
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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  
Old 23-Oct-2005, 21:13
Jbonez Jbonez is offline
New Member
 
Join Date: Jul 2005
Posts: 7
Jbonez is on a distinguished road

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:
#include <iostream>

using namespace std;
				/***********************************/
				/*** Functions and declairations ***/
				/***********************************/

void endrun();				/* ends the program  */
void display_menu(int blank_line);	/* displays the menu */
void pause_a_sec();			/* loop to pause     */
void pause_5_secs();			/* do pause loop 5 times */
void convert(float quantity);		/* case structure    */
					/*calculation function */
void clear_scrn();			/* clears the screen */
int choice;
char YesOrNo;


/********************************************/

				/***********************************/
main()		                /*** displays menu & passes choice ***/
{				/*** to the convert() function ***/
				/***********************************/

do
	{
	display_menu(0);
	printf("\n		Enter choice then press <Enter> ");
	cin >> choice;
	convert(choice);
	}

while (choice != 9);
}
			        /***************************************/
				/*** tests for end of pgm and choice ***/
				/*** out of range, gets quantity to  ***/
				/*** convert, performs calcs and     ***/
				/*** prints, using case structure    ***/
				/***************************************/
void convert(float quantity)
{

if (choice == 9)
{
clear_scrn();
endrun();
}
	if (choice < 1 || choice > 9)
	{
	printf("\n			<<< CHOICE IS OUT OF RANGE  PRESS ENTER >>> ");
	cin.get();
	cin.get();		
	clear_scrn();
	main();
	}
else
printf("\n       		Enter the quantity to be converted ");
//printf("\n            ");
cin >> quantity;
switch(choice)
	{
case 1:                       //This works fine but I really want to prompt for Enter
	printf("\n	%7.2f Inches = %7.2f Centemeters\n",
		quantity ,(quantity * 2.54));
	cout << "\n		Return to Menu - press Y and Enter " << flush;
	
	cin >> YesOrNo;
	if ((YesOrNo == 'Y') || (YesOrNo == 'y')){
	clear_scrn();
	break;
	}
	else
	endrun();
case 2:                     //This works with 2 cin.gets
	printf("\n	%7.2f Centemeters = %7.2f Inchess\n",
		quantity ,(quantity / 2.54) );
	cout << "\n       		Press ENTER to return to menu" << endl;
	cin.get();	
	cin.get();
//	pause_5_secs();
	clear_scrn();

	break;
case 3:                    //This works with 2 cin.ignores
	printf("\n	%7.2f Feet = %7.2f Meters\n",	
		quantity ,(quantity * .3048) );
	printf("\n       		Press ENTER to return to menu");
	cin.ignore();
        cin.ignore();
//	pause_5_secs();
	clear_scrn();

	break;
case 4:                    //This does NOT pause
	printf("\n	%7.2f Meters = %7.2f Feet\n",
		quantity ,(quantity * 3.2808) );
		printf("\n       		Press ENTER to return to menu");
	cin.get();
//	pause_5_secs();
	clear_scrn();

	break;
case 5:                   //This does NOT pause   
	printf("\n	%7.2f Miles = %7.2f Kilometers\n",
		quantity ,(quantity * 1.6093) );
	printf("\n       		Press ENTER to return to menu");
	cin.ignore();	
//	pause_5_secs();
	clear_scrn();

	break;
case 6:
	printf("\n	%7.2f Kilometers = %7.2f Miles\n",
		quantity ,(quantity * .6314) );
	printf("\n       		Press ENTER to return to menu");
	cin.get();	
//	pause_5_secs();
	clear_scrn();

	break;
case 7:
	printf("\n	%7.2f farenheit = %7.2f Celcius\n",
		quantity ,((quantity - 32) * 5.0/9.0) );
	printf("\n       		Press ENTER to return to menu");
	cin.get();
//	pause_5_secs();
	clear_scrn();

	break;
case 8:
	printf("\n	%7.2f Celcius = %7.2f Farenheit\n",
		quantity ,(9.0/5.0) * quantity + 32 );
		printf("\n       		Press ENTER to return to menu");
	cin.get();
//	pause_5_secs();
	clear_scrn();

	break;
	}


}

void display_menu(int blank_line)
	
					/**************************/
					/*** Prints Menu on screen***/
					/****************************/

{
printf("\n		      Acme Metric Conversion Company ");
printf("\n			         Main Menu ");
printf("\n							");
printf("\n			Your options are as follows: ");
printf("\n							 ");
printf("\n			1   Inches to Centemeters    ");
printf("\n			2   Centementers to Inches   ");
printf("\n			3   Feet to Meters           ");
printf("\n			4   Meters to Feet           ");
printf("\n			5   Miles to Kilometers      ");
printf("\n			6   Kilometers to Miles      ");
printf("\n			7   Farenheit to Celcius      ");
printf("\n			8   Celcius to Farenheit      ");
printf("\n			9   End this session         ");

for (blank_line = 1; blank_line < 8; blank_line++)
printf("\n");
}

void endrun()			/* Exits the Program */
{
printf("			<<< Exiting Program >>> ");
clear_scrn();
pause_a_sec();
exit(0);
}

void pause_a_sec()		/* Simple loop to pause a sec */
{
long x;
for (x = 1; x < 500000; x++)
;
}

void pause_5_secs()		/* runs pause_a_sec 5 times */
{
int y;
for (y = 1; y < 1500; y++)
pause_a_sec();
}

void clear_scrn()		/* Clears the screen between calcs */
{
int cls;
for (cls = 1; cls < 28; cls++)
printf("\n");
}
  #7  
Old 23-Oct-2005, 21:57
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: best way to pause program flow - cin.get?


Quote:
Originally Posted by Jbonez
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.
Which stream? The Input stream? Read this and this

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 GIDBlogHalfway done! 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
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

All times are GMT -6. The time now is 16:56.


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