I have some code that i need a press any key to continue command but when i put it in there it doesn't work, here's what i have:
cout << "Press any key to continue." << endl;
cin.get();
can anyone tell me what im doing wrong?
I have some code that i need a press any key to continue command but when i put it in there it doesn't work, here's what i have:
cout << "Press any key to continue." << endl;
cin.get();
can anyone tell me what im doing wrong?
What sort of behavior are you getting? What does the code that's before it do? Your solution to this situation lies in those details. Unfortunately, we don't have them so all that we can do is speculate at best.
search the many other threads here related to your problem.
ive tried none of them help, all they say to do is just to us cin.get(); and i try and use that but it doesnt work. all it does is skip over that. heres the code that im using.
#include <iostream>
#include <cmath>
using namespace std;
bool done = false;
int choice;
int result;
int num1;
int num2;
void add();
void sub();
void multi();
void divi();
int main() {
do{
system ("cls");
cout << "Do you want to: " << endl;
cout << "1. Add." << endl;
cout << "2. Subtract." << endl;
cout << "3, Multiply." << endl;
cout << "4. Divide." << endl;
cout << "5. Exit." << endl;
cin >> choice;
switch(choice){
case 1:
add();
break;
case 2:
sub();
break;
case 3:
multi();
break;
case 4:
divi();
break;
case 5:
done = true;
break;
}
}
while (choice != 5);
cin.get();
}
void add(){
system ("cls");
cout << "Enter the first number. " << endl;
cin >> num1;
cout << "Enter the second number. " << endl;
cin >> num2;
result = num1 + num2;
cout << "The total of " << num1 << " Plus " << num2 << " is " << result << endl << endl;
cout << "Press any key to continue." << endl;
cin.get();
}
void sub(){
system ("cls");
cout << "Enter the first number. " << endl;
cin >> num1;
cout << "Enter the second number. " << endl;
cin >> num2;
result = num1 - num2;
cout << "The total of " << num1 << " Minus " << num2 << " is " << result << endl << endl;
cout << "Press any key to continue." << endl;
cin.get();
}
void multi(){
system ("cls");
cout << "Enter the first number. " << endl;
cin >> num1;
cout << "Enter the second number. " << endl;
cin >> num2;
result = num1 * num2;
cout << "The total of " << num1 << " Multiplied by " << num2 << " is " << result << endl << endl;
cout << "Press any key to continue." << endl;
cin.get();
}
void divi(){
system ("cls");
cout << "Enter the first number. " << endl;
cin >> num1;
cout << "Enter the second number. " << endl;
cin >> num2;
result = num1 / num2;
cout << "The total of " << num1 << " Divided By " << num2 << " is " << result << endl << endl;
cout << "Press any key to continue." << endl;
cin.get();
}
whats going wrong is when the user inputs the 2 numbers, there should be a pause (the cin.get() code lines) but as soon as the user inputs the 2 numbers it starts the loop again without the user being able to see what the result was.
What happened to Narue's old pinned thread on clearing the buffer? It was great.
I won't recreate it here. The issue is that >> and get and getline act differently, so when you mix them, you often end up with whitespace remaining in what you thought was a clean buffer. When in doubt, clear the stdin stream using the ignore command
That's because you are using the "extraction operator" (>>) for numeric input. When you do this, the newline that the ENTER key puts on the input stream does not get removed from the stream. To correct this, you must place a cin.ignore() (info about istream::ignore()) after each numeric input with that operator.
...
int someNum = 0;
...
cout << "Please enter a number: ";
cin >> someNum;
cin.ignore();
...
EDIT:
@VD:
>>What happened to Narue's old pinned thread on clearing the buffer? It was great.
Agreed, I wish I knew too...
>>When in doubt, clear the stdout stream using the ignore command
Definitely, but it's actually the stdin stream that is the problem...
EDIT 2:
Found Narue's thread: http://www.daniweb.com/forums/thread90228.html
>>When in doubt, clear the stdout stream using the ignore command[/B]
Definitely, but it's actually the stdin stream that is the problem...
Edited it after posting and before seeing your post. Scout's Honor. :) Good catch.
ok so now i got this to work but it only continues when i press enter. so how would i make it so all theuy have to do is press any key? so one person could press A to continue and someone else could press P etc.
As far as I know, there is no input operation in Standard C/C++ that behaves that way. You'll probably have to use an o/s-specific API call for that. But I don't know any o/s APIs just yet.
ahh ok so ill just have to say type "y" to continue or something like that for now
You don't. The C++ standard does not allow for "hit any key" type input. You need to change your prompt to "Press ENTER to continue"
i took what you said and made a simple test program to see if it would work:
#include <iostream>
using namespace std;
int main(){
int choice;
bool done = false;
do {
system("cls");
cout << "Do you want:\n";
cout << "1. Pie. \n";
cout << "2. Cookie's.\n";
cout << "3. Ice Cream.\n";
cout << "4. Exit.\n \n";
cin >> choice;
cout << "\n";
switch (choice){
case 1:
cout << "You chose Pie.\n \n";
cout << "Press enter to continue. \n";
cin.ignore();
cin.get();
break;
case 2:
cout << "You chose Cookie's.\n \n";
cout << "Press enter to continue. \n";
cin.ignore();
cin.get();
break;
case 3:
cout << "You chose Ice Cream.\n \n";
cout << "Press enter to continue. \n";
cin.ignore();
cin.get();
case 4:
done = true;
}
}while (done != true);
cout << "Press enter to exit. \n";
cin.ignore();
cin.get();
}
it seems to work in this sense so if i were to use this in a full program it should work the same way?
Yes. But why end all cases with the same 3 statements? Put them once at the bottom of the loop after the switch structure.
so change it to:
#include <iostream>
using namespace std;
int main(){
int choice;
bool done = false;
do {
system("cls");
cout << "Do you want:\n";
cout << "1. Pie. \n";
cout << "2. Cookie's.\n";
cout << "3. Ice Cream.\n";
cout << "4. Exit.\n \n";
cin >> choice;
cout << "\n";
switch (choice){
case 1:
cout << "You chose Pie.\n \n";
break;
case 2:
cout << "You chose Cookie's.\n \n";
break;
case 3:
cout << "You chose Ice Cream.\n \n";
case 4:
done = true;
}
cout << "Press enter to continue. \n";
cin.ignore();
cin.get();
}while (done != true);
cout << "Press enter to exit. \n";
cin.ignore();
cin.get();
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.