Char array not recognize certain letters

int main()
{
const int Teachers_Array= 20; //Array Size
const int Student_Array=20; // Array Size

char Teachers_Exam[Teachers_Array] = {'B','D','A','A','C','A','B','A','C','D',
'B','C','D','A','D','C','C','B','D','A'}; // Holds Teachers Answers
char Student_Exam[Student_Array]; // Holds Students Answers


// Introduction and Explanation to the program for the user.
Exam_Intro();

// Answers of Student.
cout<< "Please type in the answers of your student:"<<endl;
// For loop to cycle through all 20 questions
// and put each question in the array element.
for (int count=0; count<Teachers_Array; count++)
{
cout<<"Question #"<<(count+1)<< ". ";
cin>>(Student_Exam [count]);
cin.ignore();

}
// For loop using toupper to cycle through
//each element and make it an uppercase.
for (int x=0; x< Student_Array; x++)
{
Student_Exam[x]= toupper (Student_Exam[x]);
}
return 0;
}



So I think I corrected my toupper problem, but I was now wondering how to make this program not recognize letters other than A, B, C, D. I keep trying to use the while loop for that but I keep getting errors

while( Student_Exam != 'A' && Student_Exam != 'B' etc...) it says that I'm comparing two different types of char
Student_Exam != 'A' is invalid because Student_Exam is not a character. Student_Exam[0] is a character. So is Student_Exam[1] and so forth...
Last edited on
Ah, ok. So I'll need another loop that checks each element in the array

Thanks booradley
Last edited on
Or you could not allow input outside the range a-d like so:
Define a function like this...
1
2
3
4
5
6
7
8
9
10
bool isValidGrade(char c)
{
    if((c >= 'a' && c <= 'd') ||
       (c >= 'A' && c <= 'D'))
    {
        return true;
    }

    return false;
}


Then change the loop where you get user input to look like this...
1
2
3
4
5
6
7
8
9
10
11
12
13
for (int count=0; count<Teachers_Array; count++)
    {
        char grade;
        while ((cout<<"Question #"<<(count+1)<< ". ")
            && (!(cin >> grade) || !isValidGrade(grade)))
        {
                std::cout << "That's not a grade between A and D.\n";
                std::cin.clear();
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
        std::cin.sync();
        Student_Exam [count] = grade;
    }
Last edited on
Ah ok Booradley thank you. I should have came back and saw this earlier.
I ended up using:

while( Student_Exam[count] != 'A' && Student_Exam[count] != 'B' && Student_Exam[count]!= 'C' && Student_Exam[count] != 'D')
{
cout<< "Please type in a letter A , B, C, or D."<<endl;
cin>> (Student_Exam[count]);
}

I found out this while loop did work, however last time I was making it wrong and put it in the wrong place in my function.

Topic archived. No new replies allowed.