Help with case sensitivity!

Hello everyone. So here is the assignment: Your program should store the correct answers shown above in an array. It should ask the user to enter the student’s answers for each of the 20 questions, and the answers should be stored in another array. After the student’s answers have been entered, the program should display a message indicating whether the student passed or failed the exam. (A student must correctly answer 15 of the 20 questions to pass the exam.) It should then display the total number of correctly answered questions, the total number of incorrectly answered questions, and a list showing the question numbers of the incorrectly answered questions. Your program must have at least the following functions:

// Function prototype:

void input(char [], int);
void checkAnswers(char[], char[], int &, int &, int);
*. The input function accepts an array of characters and an integer for the size of the array. The function asks the user to input answers to the exam and stores them in the array. You must have an Input Validation loop: Only accept the letters A, B, C, D or a, b, c, d as answers.

*. The checkAnswers function compares the values in the answers array to the values in the replies array. The number of correct and incorrect answers are stored in the correct and incorrect reference parameters.

So I got everything working out just fine, EXCEPT for the fact that I want the student input to be case-insensitive; meaning that even if they input "a" it would still be correct even though the answer is "A" as shown in the answers array. I would really appreciate your help, as this assignment is due tonight.


And this is my code:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;

void input(char [], int);
void checkAnswers (char answers[], char stuInput[], int& questions, int& minCorrect);


int main()
{
int questions = 20;
int minCorrect = 15;

char answers [20] = {'A','D','B','B','C','B',
                     'A','B','C','D','A','C',
                     'D','B','D','C','C','A','D','B'};

char stuInput [questions];

input (stuInput, questions);
checkAnswers (answers, stuInput, questions, minCorrect);

return 0;
}

void input(char stuInput[], int questions)
{
cout << "Please enter the student's answers for each question." << endl;
cout << "Please Enter after typing each answer." << endl;
cout << "Please enter only an A, B, C, D, or a, b, c, d, for each question." << endl;
// Loop for users answers
for (int answ = 0; answ < questions; answ++) {
cout << "Question " << (answ+1) << ": ";
cin >> stuInput[answ];


// Validation for user answers
while (stuInput[answ] != 'A' && stuInput[answ] != 'B' &&
       stuInput[answ] != 'C' && stuInput[answ] != 'D' &&
       stuInput[answ] != 'a' && stuInput[answ] != 'b' &&
       stuInput[answ] != 'c' && stuInput[answ] != 'd')
    {
    cout << "Use only A, B, C, D or a, b, c, d!\n";
    cout << "Please try again." << endl;
    cout << "Question " << (answ+1) << ": ";
    cin >> stuInput[answ];
    }
}
}

void checkAnswers (char answers[], char stuInput[], int& questions, int& minCorrect)
{
int correctAnswers = 0;

//Check the student's replies against the correct answers
for (int i = 0; i < questions; i++)
{
if (answers[i] == stuInput[i])
correctAnswers++;
}

//Pass or fail?
if (correctAnswers >= minCorrect)
{
cout << "\nThe student passed the exam.\n";
}
else
{
cout << "\nThe student failed the exam.\n";
}

//Display list of questions incorrectly answered
cout << "\nQuestions that were answered incorrectly:\n";

for (int i = 0; i < questions; i++)
{
if (answers[i] != stuInput[i])
cout << i << endl;
}

//Display the number of correct and incorrect answers
cout << "\nCorrect answers: " << correctAnswers << endl;
cout << "\nIncorrect answers: " << questions - correctAnswers << endl;
}
Last edited on
What exactly is your question? Have you tried to implement case-insensitivity, but it's not working like you expected? Will the program not allow lower-case input? Will it accept it, but will still mark the answers as wrong? Or have you simply not attempted to implement it at this point?

I haven't attempted to implement case-insensitivity because I don't know how to..
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
#include <iostream>
#include <cctype>

// get valid input from user. on invalid input, try again
char get_answer( int question_number )
{
    std::cout << "Answer to question #" << question_number << "? " ;

    char ans ;
    std::cin >> ans ;
    
    // http://www.cplusplus.com/reference/cctype/toupper/
    ans = std::toupper(ans) ; // convert to upper case if it is a lower case letter
    if( ans == 'A' || ans == 'B' || ans == 'C' || ans == 'D' ) return ans ; // valid answer

    // invalid answer
    std::cout << "Use only A, B, C, D or a, b, c, d!\nPlease try again.\n" ;
    return get_answer(question_number) ; // try again
}

void input( char stuInput[], int num_questions )
{
    std::cout << "Please enter the student's answers for each question.\n"
                 "Please Enter after typing each answer.\n"
                 "Please enter only an A, B, C, D, or a, b, c, d, for each question.\n" ;

    // Loop for users answers
    for( int i = 0; i < num_questions; ++i ) stuInput[i] = get_answer(i+1) ;
}
Thank you!!
Topic archived. No new replies allowed.