Writing a Press Enter to Continue Function

Member Avatar

There are several ways of doing what you want to do here. I'm sure that I have omitted some, but these will work:

              char digit=0;     int digit2 =0;     cout<<"Enter a digit: ";     digit = cin.get(); //assigns whatever (single) character the user enters to variable 'digit'     //if they enter more characters it will not use them for this variable but keep them in the buffer     //and act as if the user entered whatever's in the buffer, the next time it wants input          cout<<"Enter 2nd digit: ";     cin>>digit2; //assigns whatever (int) number the user enters to variable 'digit2'. Doesn't [I]have[/I] to be an int, but that's how I declared digit2 so that's what this one will be          cout<<endl<<digit<<endl<<digit2<<endl;          cout<<"Press any key: "; //this may be the one you're looking for, but...     getch();  //need to #include <conio.h>, though I've been told it's not a good thing to do...but it works            

Member Avatar

Member Avatar

edit: @Myrtle, just cin.get() or even getchar() instead of the decrepit getch().

yes, I have heard this before...but getchar(); NEVER works for me! It always just skips right over it, as if getchar(); is not even in my code.

Member Avatar

yes, I have heard this before...but getchar(); NEVER works for me! It always just skips right over it, as if getchar(); is not even in my code.

What compiler might you be using?

Member Avatar

I might be using anything...but I am using Dev-C++ for now. I also have VS C++ 2008 Express, but haven't used it much yet.

I think I know what my problem is with getchar. What do you think?

              char digit=0;     int digit2 =0;     cout<<"Enter a digit: ";     digit = cin.get();           cout<<"Enter 2nd digit: ";     cin>>digit2;           cout<<endl<<digit<<endl<<digit2<<endl;          cout<<"Press any key:\n ";      digit = getchar();  //compiler skips right over this    // cout<<digit<<endl;     digit = getchar();//this one works, only after it skips the 1st one...something left in the buffer maybe? like the endline character? or whatever is left from pressing enter after entering the digits above?     cout<<digit<<endl;     cout<<"Press any key:\n ";      digit = getch();       cout<<"Digit: "<<digit<<endl; //this works            

Member Avatar

Meh...

Whenever I don't feel like writing that whole system pause thing, I do this:

              #define pause system("PAUSE");            

so then its usage would be:

              pause;            

Member Avatar

Whenever you mix the >> operator and getline or get or getchar or whatever, you have that pesky whitespace that one leaves behind and the other treats as valid, so you either have to use cin.get() twice (first to get the newline character, then to pause) or you need to make sure the input buffer is clear. See Narue's thread pinned to the C++ forum on how to do this.

As for system("PAUSE") and the arguments against it, see this writeup by WaltP.

http://www.gidnetwork.com/b-61.html

Member Avatar

Narue has explained this in great detail here. Her post includes a pretty comprehensive solution!

Cheers for now,
Jas.

EDIT: Ooops, looks like Vernon beat me to the punch there!

Nicely done Vernon. A bit more verbose than my entry!

Member Avatar

I might be using anything...but I am using Dev-C++ for now. I also have VS C++ 2008 Express, but haven't used it much yet.

In addition (if it hasn't been pointed out yet) Mixing cin with getchar() should not be done. Stick to one style.

Meh...

Whenever I don't feel like writing that whole system pause thing, I do this:

                #define pause system("PAUSE");              

so then its usage would be:

                pause;              

This is one of the worst suggestions I've ever seen... How does this eliminate using system("PAUSE") as everyone else is suggesting? :icon_rolleyes:

Member Avatar

Mixing cin with getchar() should not be done.

Okay. But what if you want to use cin for your program, but then you want to pause the screen without using

              system("pause");            

?

I expect you'd use cin.get() ? Is it better to do that or use getchar? Or does it depend on the program?

Apparently you still have to clear the buffer (or get it to ignore whatever's in there) in order for cin.get() to actually pause the screen.

Member Avatar

Okay. But what if you want to use cin for your program, but then you want to pause the screen without using

                system("pause");              

?

I expect you'd use cin.get() ? Is it better to do that or use getchar? Or does it depend on the program?

Why use a C function when the equivalent C++ function is available?

Apparently you still have to clear the buffer (or get it to ignore whatever's in there) in order for cin.get() to actually pause the screen.

Naturally. If you input a single character, one character is input, no matter what language you are writing in. If you press the ENTER, it's goes into the buffer. You have to get rid of it to read the next character.

rondoncalawas42.blogspot.com

Source: https://www.daniweb.com/programming/software-development/threads/269277/press-any-key-to-continue-function

0 Response to "Writing a Press Enter to Continue Function"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel