The user can theoretically type in bad input, such as "abc", so you need to be able to capture this input (multiple characters). So, your 'letter' should be a string as well (think of it as the "lowest common denominator" of input the user could give).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
string answer;
cin >> answer;
if (answer == "Y" || answer == "y")
{
// valid, positive
}
else if (answer == "N" || answer == "n")
{
// valid, negative
}
else
{
// invalid input
}
|
PS: Your formatting is misleading. Your if statement and body is only line 17 (the body being "validinput = true;"). Lines 18 to 23 are not part of your if statement. This applies to all three of your conditions you check.
This is how the compiler sees your code as it is now:
1 2 3 4 5 6 7 8 9 10 11 12
|
if (letter == 'y')
{
validinput = true;
}
// unconditional:
{
cout << "Enter your first and last name: " << endl;
cin.ignore();
getline(cin, fullname);
cout << "Hello " << fullname << ". " << "You are enrolled in college courses. " << endl;
}
|
You probably want
1 2 3 4 5 6 7 8
|
if (letter == 'y')
{
validinput = true;
cout << "Enter your first and last name: " << endl;
cin.ignore();
getline(cin, fullname);
cout << "Hello " << fullname << ". " << "You are enrolled in college courses. " << endl;
}
|
PPS:
else if (letter != 'n' || 'y')
This is a common mistake. Each part of the statement in a boolean expression must be independent. You meant to say:
else if (letter != 'n' || letter != 'y')
However, this is also not right, because that statement is always true. You actually meant:
else if (letter != 'n' && letter != 'y')
However, this is redundant because you already checked for valid input, so this can just be an else statement, if line 25 is changed to an else if.