-
Please Read This!
whenever the user of my program hits ENTER it shuts down. how should the user input their age without closing the program? (i am using bloodshed dev-C++ in case your wondering)
my code is:
#include <iostream.h>
using namespace std;
int main (int argc, char *argv)
{
int age;
cout << "please input your age:";
cin >> age;
if(age<100)
{
cout << "you are pretty young";
}
else if(age==100)
{
cout << "you are old";
}
else
{
cout << "you are really old";
}
return 0;
}
-
1) when posting code to this board use code tags to make your code more readable.
2) add something like:
cin.get();
just before the line
return 0;
-
it still has the same problem. is there something the user should press (other than enter, apparently) so that the program reads it?
-
before cin.get(); you must clear the '\n' that still remains in the
stream, how you clear it? with the cin.ignore();
here's the complete code:
Code:
#include <iostream.h>
using namespace std;
int main (int argc, char *argv)
{
int age;
cout << "please input your age:";
cin >> age;
if(age<100)
{
cout << "you are pretty young";
}
else if(age==100)
{
cout << "you are old";
}
else
{
cout << "you are really old";
}
cin.ignore();//clear the new line character
cin.get();//wait for input
//you can use : system("pause") too, but is not that portable
//and you dont need the cin.ignore if u use system("pause")
//dont forget to include the <stdlib>header when using,
//system("pause");
return 0;
}
please excuse my poor english
-
Try:
char ch;
cin >> ch;
instead of
cin.get();
I'm probably giving you the wrong syntax of get(). I actually use the cin >> ch; version, personally, but the other version, with correct syntax, seems to be the most common recommendation from the board. You should find plenty of posts by searching.
If that doesn't work either edit your original post, adding code tags and the modification to your code, or repost your code, using code tags---read the sticky message at the top of the board to learn about code tags.
-
two problems preventing me from reading this:
1- Please Read This!
2- No Code Tags!
-
Not only was the title useless (did you read the posting guidelines?), you didn't read the FAQ either
http://faq.cprogramming.com/cgi-bin/...&id=1043284385