0

I've been looking around the site for an answer to this, only thread i've found pertaining to it was this:

http://www.daniweb.com/forums/showthread.php?t=96516&highlight=how+do+i+get+my+function+to+ask+a+question+and+wait+until+the+user+presses+a+key+in+C%2B%2B%3F&page=2

and that thread wasn't very helpful, so, i'm sorry if it's been asked before somewhere.

anyway, onto my question:

The title was pretty self explanatory, I want to know a specific function that I can use to make it so that the user has to press a key before the program continue's running, *AND*, I want to be able to dictate what is said when it asks the user to press any key. So instead of it saying "Press any key to continue", I can make it say "Enter a digit" or whatever I want it to say.

Right now I'm using the function System ("PAUSE"), and I'd prefer not to use this.

Thanks for any assistance.

Edited by Asthuran: n/a

7
Contributors
11
Replies
13
Views
7 Years
Discussion Span
Last Post by WaltP
Featured Replies
  • 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 … Read More

  • 1
    WaltP 2,905   7 Years Ago

    [QUOTE=MyrtleTurtle;1165481]I [I]might [/I]be using anything...but I [I]am [/I]using Dev-C++ for now. I also have VS C++ 2008 Express, but haven't used it much yet.[/QUOTE] In addition (if it hasn't been pointed out yet) Mixing [ICODE]cin[/ICODE] with [ICODE]getchar()[/ICODE] should not be done. Stick to one style. [QUOTE=CreativeCoding;1165582]Meh... Whenever I don't feel … Read More

0

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

Edited by MyrtleTurtle: n/a

0

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.

0

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?

0

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
0

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;

Edited by CreativeCoding: lol, do do

1

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

0

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!

Edited by JasonHippy: n/a

1

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:

Votes + Comments
That's why I like sites like this. There is so much they don't teach in class. Thanks for the info.
0

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.

Edited by MyrtleTurtle: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.