urgent help with input problem

need help urgent.

if the user enters y, or Y
output = you are enrolled

if the user enters n, or N
output = you are not

if the user enters anything else "yes, YES, no, NO, No, Yes, abc, etc"
output = bad input


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
using namespace std;

int main()
{
string fullname;
char letter;
bool validinput;


    cout << "Are you enrolled in college courses (y,Y,n,N): " << endl;
    cin >> letter;  
    letter = towupper(letter);

         
    if (letter == 'y') validinput = true; // if the letter is equal to 'Y' or 'y'
    {
        cout << "Enter your first and last name: " << endl;
        cin.ignore();
        getline(cin, fullname);
        cout << "Hello " << fullname << ". " << "You are enrolled in college courses. " << endl;
    }

    if (letter == 'n') validinput = true; // if the letter is equal to 'N' or 'n'
    {
        cout << "Enter your first and last name: " << endl;
        cin.ignore();
        getline(cin, fullname);
        cout << "Hello " << fullname << ". " << "You are not enrolled in college courses. " << endl;
    }
    else if (letter != 'n' || 'y') validinput = false; // if they enter 'Yes, YES, no, NO, No, etc. anything from " y, Y, n, N" it should break. 
    {
        cout << "Enter your first and last name: " << endl;
        cin.ignore();
        getline(cin, fullname);
        cout << letter << "is an invalid input " << fullname << "." << endl;
        cout << "The last letter of your last name is: ";
        cout << fullname[fullname.length() - 1] << endl;

    }
        
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.
Last edited on
Topic archived. No new replies allowed.