Hey, I'm just getting started with learning c++ and put together a quick practice program to get myself used to input/output and if/else. I'm having an issue with the if/else statement where even when the conditions are met, the program is registering as false and continuing onto else. I've tried looking up the issue and haven't found an answer, and can only guess at this point that is has something to do with the yorn variable being a string.
#include <iostream>
usingnamespace std;
int main(){
char name[100], yorn[5];
cout << "What is your name?" << endl;
cin.getline (name, 100);
cout << "Hello, " << name << "!" <<endl;
int age;
cout << "How old are you?" << endl;
cin >> age;
cout << "Have you had a birthday yet this year?" << endl;
cin.ignore();
cin.getline (yorn, 5);
if (yorn == "yes" || yorn == "Yes"){ //for some reason, always registers false and continues to else
cout << "You were born in the year " << 2019-age << "." << endl;
system ("PAUSE");
return 0;
}
else{
cout << "You were born in the year " << (2019-age)-1 << "." << endl;
system ("PAUSE");
return 0;
}
return 0;
}
Thanks, was able to figure it out because of that. I've gotten used to python and had been under the assumption that char and string were the same thing, so I'd been saving yorn as a char. Changing it to a string solved the issue!