Array input validation

I have been messing with this for some time and I cannot figure out why this code is not working. I am trying to make the user input either A, B, C, or D. Right now when I put one of these it works and if I put in, lets say "s" it works. However, when I put in AA it will skip to test score for test number 3, or if I put ssss it will display the error 4 times.The user should only be able to enter one letter either A, B, C, or D. Thanks so much for your help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 for (count = 0; count < size; count++)
    {
        if  (!space) // extra code to help with spacing
        {
            cout << "\n";
        }

        cout <<  "What is your score for test " << (count + 1) << " ? ";
        cin >> studentAnswers[count];

        while ((studentAnswers[count] != 'A' && studentAnswers[count] != 'B' && 
                studentAnswers[count] != 'C' && studentAnswers[count] != 'D'))
        {
            cout << "You must enter A, B, C, or D. ";
            cin >> studentAnswers[count];
            space = false;
        }
    }
I assume that studentAnswers[count] is a char. If you ask cin for a char, it gives you a char. If there are two chars in the input stream, it can only give you one, since you only asked for one.
The solution would be to use a temporary string. Call getline to read the whole line. Then either compare tat string with "A", "B" and etc. or do studentAnswers[count] = my_string[0];. It depends on whether you consider input "Ablahblahblah" valid or not.
Yeah, input "Ablablahblah" I would want to be invalid.
Also yes it is a char. I tried what you said and replaced my cin with cin.getline(studentAnswers, count, but that completely messed the code up.

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
int main()
{
    const int size = 20; // Array size
    char    answers[size] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D',
                            'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'},
        studentAnswers[size];// Holds the line of user input
    int count = 0;
    int sCount = 0;
    int incorrect = 0;
    bool space = true;

    for (count = 0; count < size; count++)
    {
        if  (!space) // extra code to help with spacing
        {
            cout << "\n";
        }

        cout <<  "What is your score for test " << (count + 1) << " ? ";
        cin >> studentAnswers[count];

        while ((studentAnswers[count] != 'A' && studentAnswers[count] != 'B' && 
                studentAnswers[count] != 'C' && studentAnswers[count] != 'D'))
        {
            cout << "You must enter A, B, C, or D. ";
            cin >> studentAnswers[count];
            space = false;
        }
    }

    processAnswers(studentAnswers, answers, size);

    system("pause");
    return 0;
}
You already use studentAnswers for other things. Example:
replace
1
2
3
4
5
6
7
8
cin >> studentAnswers[count];

while ((studentAnswers[count] != 'A' && studentAnswers[count] != 'B' && 
        studentAnswers[count] != 'C' && studentAnswers[count] != 'D'))
{
    cout << "You must enter A, B, C, or D. ";
    cin >> studentAnswers[count];
}
with
1
2
3
4
5
6
7
std::string temp;//could be a char array, but strings are nicer
getline(cin, temp);
while(temp != "A" && temp != "B" /*and so on. note the double quotes*/){
    cout << "You must enter A, B, C, or D. ";
    getline(cin, temp);
}
studentAnswers[count] = temp[0];//this line will only be reached when input is valid. 
by the way. I'd suggest a different structure:
1
2
3
4
5
6
7
8
9
while(true){
    std::string temp;
    getline(cin, temp);
    if(temp == "A" || temp == "B" || temp == "C" || temp == "D"){
        studentAnswers[count] = temp[0];
        break;
    }
    cout << "You must enter A, B, C, or D. ";
}
that way you don't have two pieces of code for input.
Last edited on
Topic archived. No new replies allowed.