Hello,
On line 14, I expected the console to stay open long enough for the user to press a key while displaing some text. When I run this (using Visual Studio Express 2010), it simply closes the console upon inputting "53".
#include <iostream>
usingnamespace std;
int main()
{
int n = 1;
int guess, answer = 53;
cout << "Guess the number (1-100):";
do {
cin >> guess;
if (guess == answer) {
cout << guess << " is the correct answer!";
cin.get();
n = 0;
}
else
cout << "Guess again:";
} while (n != 0);
return (0);
}
I noticed that if I duplicate line 14, then the console awaits input, then closes. What would be the correct way to keep the console open (without using system("Pause"))?
Ah. I think I get it now. Though it's true that it is a console program, the intention I want is for the executable to be run outside of the console (via windows explorer or other).
So would be it logical that I clear the buffer prior to line 14?
#include <iostream>
usingnamespace std;
int main()
{
int n = 1;
int guess, answer = 53;
cout << "Guess the number (1-100):";
do {
cin >> guess;
if (guess == answer) {
cout << guess << " is the correct answer!";
n = 0;
}
else
cout << "Guess again:";
} while (n != 0);
cin.get(); cin.get();
return 0;
}
Just to throw in my two cents. Whatever you decide to use, make it into a function. The first thing I type into any new project I start these days is this: