Hi, im just started learning c++, and this is just a part of the code of my own 'exercise'.
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
string name;
int age;
int choice;
printf("Hi! What is your name? \n");
getline(cin,name);
system("cls");
cout << "Hello " <<name << " !"<<endl;
cout << "How old are you?\n";
cin >> age;
system("cls");
i would like to know how to prevent people from entering characters as the 'age' because that would cause a bug to happen.
Yep, you've discovered one of the weaknesses in the cin object. The best way around this is to read the user input as a string, and then convert it using atoi().