| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Pausing the program
Hi,
This is my first post, but I am really ripping my hair out here as I can't see what is going wrong. Heres the thing I want my program to wait for the user to push any key before the program calls the main() function again I am using getchar() to do this . Heres the bit of code that is causing the problem (I havn't been doing this for long so the code is probably a bit rough, but it should work) Code:
while (x=1)
{
cin >> LD;
if (LD >= 1 && LD <= 12)
{
cout << "\n\n You must roll lower or equal to " << LD << " to pass the test and not break. Good Luck\n";
break;
}
else
{
cout <<"\nPlease enter a number between 1 and 12 for the LD statistic";
continue;
}
}
getchar();
main();
}
The program does pause, but it pauses when the user enters a correct number beforre it has displayed the text << "\n\n You must roll lower or equal to " << LD << " to pass the test and not break. Good Luck\n"; to the screen. It will then wait for a keystroke before displaying the text and returning to main(). I have also tried using the system("pause") command but this gives me exactly the same results. I am using MS Visual Studio 6.0 to compile it. Any help would be much appriciated thanks very much for reading. Ad. Last edited by B-Con : April 16th, 2005 at 09:30 PM. Reason: don't forget to use the [code] tags ;) |
|
#2
|
|||
|
|||
|
I am a bit confused on what you're trying to accomplish, but I see one hidden bug at the top.
Code:
while (x=1)
I am guessing, but I assume you really mean: Code:
while (x==1) Because you are effectively setting x=1 in each loop. In addition, cin captures the character and any extra characters like the carraige return. I would suggest you do something like Code:
cout << endl; before you call your getch() function. The endl manipulator essentially does a carraige return followed by a flush of the buffer. Then your getch() function should be able to wait until a key is pressed. Hope this helps! |
|
#3
|
|||
|
|||
|
Thanks that worked a treat!!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Pausing the program |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|