468,736 Members | 2,167 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,736 developers. It's quick & easy.

USING pause in C++ program

22
I've used the command "pause" in batch files which works ok but I guess it's applied dirrently in c++.
Using Dev C++ 4.0 and the following is my code

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<iomanip>
  3. #include<cstdlib>
  4. #include<string>
  5.  
  6. main(){
  7.  
  8.  
  9.     do{
  10.         display menu
  11.         cin>>getInput
  12.         if{
  13.  
  14.         }
  15.         else if{
  16.  
  17.         }
  18.         else{
  19.                 cout<<"\n\t"<<mnuSelect<<" is not a valid menu"
  20.                             <<" selection"
  21.                             <<"\n\tHit ENTER to return to menu and try again";
  22.                             system("cls");
  23.                     pause;
  24.                 }
  25.         }while(mnuSelect != 'x' && mnuSelect != 'X');
  26.         return 0;
  27. }
I keep getting the following error whenever I use the pause;

c:\test.cpp: In function `int main()':
c:\test.cpp: lineNum: `pause' undeclared (first use this function)
c:\test.cpp: lineNum: (Each undeclared identifier is reported only once
c:\test.cpp: lineNum: for each function it appears in.)

For ANY false input, my result is to pause with error MSG and ask user to press enter which will return to main to re-enter valid menu selection.
How do I get pause working?
Thanks to all. Await reply
Apr 22 '07 #1
8 10909
Savage
1,764 Expert 1GB
There is no such thing in c++ as this:

pause;

Pause declaration is:

void __fastcall Pause(void)

and it's used for media players to switch beetwen play,stop and pause.

use instead system("pause") or kbhit() or something like that.


Savage
Apr 22 '07 #2
AdrianH
1,251 Expert 1GB
You have a few problems here.

Re: pause problem

There is no function called pause. If you want a standard portable way of waiting for the user to hit the enter key, use the standard stream facility that you are using for your input. Try using the istream::getline() function (see http://www.cplusplus.com/reference/i...m/getline.html for more information).

Re: other problems

For ANY false input, my result is to pause with error MSG and ask user to press enter which will return to main to re-enter valid menu selection.

Expand|Select|Wrap|Line Numbers
  1. cout<<"\n\t"<<mnuSelect<<" is not a valid menu"
  2.     <<" selection"
  3.     <<"\n\tHit ENTER to return to menu and try again";
  4. system("cls");
  5. pause;
Think about this code fragment that you posted and then think if that is in the order that you want it to be done in.

Note that system("cls") is not a portable command, though if you stick to windows, it should be fine.

Hope this helps,


Adrian


P.S. Please use code tags when posting code fragments. Thanks.
Apr 22 '07 #3
Lloydm
22
Thanks guys, wasn't able to get it going with istream::getline() but system("pause"); works as well as getchar() works quite fine. However just testing a bit further, entering two or more input with spaces will have me press enter for each as error


ex

string mnuSelect=" ";

cout<<"\n Enter menu selection (A, B, C, D, or E to EXIT): ";
cin >>mnuSelect;

if I entered : the quick brown fox as mnuSelect, I''ll get an error and would have to press enter for each word i typed. is it better to store mnuSelect as array or use fflush(stdin) in code?
Apr 29 '07 #4
Savage
1,764 Expert 1GB
Thanks guys, wasn't able to get it going with istream::getline() but system("pause"); works as well as getchar() works quite fine. However just testing a bit further, entering two or more input with spaces will have me press enter for each as error


ex

string mnuSelect=" ";

cout<<"\n Enter menu selection (A, B, C, D, or E to EXIT): ";
cin >>mnuSelect;

if I entered : the quick brown fox as mnuSelect, I''ll get an error and would have to press enter for each word i typed. is it better to store mnuSelect as array or use fflush(stdin) in code?
Sorry,but I'm not sure that I understood u well.Is user entering just one char or whole string?


Can u be a little bit accurate with ur question?

Savage
Apr 29 '07 #5
Lloydm
22
Sorry,but I'm not sure that I understood u well.Is user entering just one char or whole string?


Can u be a little bit accurate with ur question?

Savage

The expected user input should be 1 char. You may have a user enter say Bspace C which of course is incorrect. mmmm. Am I going about the wrong way of using string instead of char[ ]? Isn't string to show entire input entered vs char?
Apr 29 '07 #6
Savage
1,764 Expert 1GB
The expected user input should be 1 char. You may have a user enter say Bspace C which of course is incorrect. mmmm. Am I going about the wrong way of using string instead of char[ ]? Isn't string to show entire input entered vs char?
Yes,string shows entire input.To test for error just test for the string lenght.If it's larger of 1 input is incorrect.

And also this is a erorr:string mnuSelect=" ".If u wish to create empty string use string terminator '\0' character instead.

Savage
Apr 29 '07 #7
Lloydm
22
Yes,string shows entire input.To test for error just test for the string lenght.If it's larger of 1 input is incorrect.

And also this is a erorr:string mnuSelect=" ".If u wish to create empty string use string terminator '\0' character instead.

Savage
Ok, Thanks for your feedback, however I tried the following code but got error which I pasted below

Expand|Select|Wrap|Line Numbers
  1. else{
  2.                      if(mnuSelect.length> 0) {
  3.                             cout<<error msg";
  4.                      }
  5.                      system("pause");
  6.       }
ERROR
line # c:\test.cpp
invalid operands `{unknown type}' and `int' to binary `operator >'

What now?
Apr 29 '07 #8
AdrianH
1,251 Expert 1GB
Ok, Thanks for your feedback, however I tried the following code but got error which I pasted below

Expand|Select|Wrap|Line Numbers
  1. else{
  2.                      if(mnuSelect.length> 0) {
  3.                             cout<<error msg";
  4.                      }
  5.                      system("pause");
  6.       }
ERROR
line # c:\test.cpp
invalid operands `{unknown type}' and `int' to binary `operator >'

What now?
You forgot the () after .length. Also the leading " for "error msg".

You should flush the input stream to ensure that you don't get garbage prior to reading from it.


Adrian
Apr 30 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

31 posts views Thread by Developwebsites | last post: by
11 posts views Thread by Paminu | last post: by
8 posts views Thread by Wim | last post: by
6 posts views Thread by Mark Broadbent | last post: by
9 posts views Thread by Animatorboy | last post: by
reply views Thread by FaroeIslander | last post: by
18 posts views Thread by Andrew Gentile | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.