I have a program I made that will check your age and gender. I am using visual studio 2022 to see what happens when debugging.
If I put random letters for the age it skips the cin > gender part which is so weird and the locals shows that age is set to 0 but gender is -52'' . What can I do so it still atleast takes your gender, even if you put weird stuff in for age?
#include <iostream>
using namespace std;
int main()
{
// prompt user for age AND gender
//age is int, gender is character
int age;
char gender;
cout << "What is your age? \n" << endl;
cin >> age;
cout << "What is your gender? \n" << endl;
cin >> gender;
if (gender == 'f' || gender == 'F')
{
bool result = (age > 60);
switch (result)
{
case 0:
cout << "You do not qualify for discount need to be over 60";
break;
case 1:
cout << "You qualify for discount";
break;
}
}
else if (gender == 'm' || gender == 'M')
{
cout << "You do not qualify you need to be female and over 60 \n" << endl;
}
else {
cout << "Wrong gender either use male or female Exiting... \n" << endl;
return -1;
exit;
}
return 0;
}
This is a picture of the visual studio locals area