Help with input validation

So I've got this whole program working but basically I need to insert something in the code that says after three attempts at putting an invalid answer (anything other than A, B, C, or D) the program says "Goodbye!" and exits the program. I'm fairly certain its supposed to go in this for loop, but I'm not really sure how to go about it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for (int replies = 0; replies < NUM_QUESTIONS; replies++)
	{
		cout << "Please enter your answers: "
			<< (replies + 1) << ": ";
		cin >> studentAnswers[replies];

		//Validation of users answers
		while (studentAnswers[replies] != 'A' && studentAnswers[replies] != 'B' && studentAnswers[replies] != 'C' && studentAnswers[replies] != 'D')
		{
			cout << "You must enter A, B, C, or D\n";

			cout << "Please enter your answers: "
				<< (replies + 1) << ": ";
			cin >> studentAnswers[replies];

		}

	}

But anyways, here's the whole program:
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
#include <iostream>
#include <cctype>

using namespace std;

void grade(char[], char[], int, int);


int main()
{
	const int NUM_QUESTIONS = 10;
	const int MIN_CORRECT = 8;
	char correctAnswers[NUM_QUESTIONS] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};

	char studentAnswers[NUM_QUESTIONS];

	//Loop for users answers
	for (int replies = 0; replies < NUM_QUESTIONS; replies++)
	{
		cout << "Please enter your answers: "
			<< (replies + 1) << ": ";
		cin >> studentAnswers[replies];

		//Validation of users answers
		while (studentAnswers[replies] != 'A' && studentAnswers[replies] != 'B' && studentAnswers[replies] != 'C' && studentAnswers[replies] != 'D')
		{
			cout << "You must enter A, B, C, or D\n";

			cout << "Please enter your answers: "
				<< (replies + 1) << ": ";
			cin >> studentAnswers[replies];

		}

	}

	grade(correctAnswers, studentAnswers, NUM_QUESTIONS, MIN_CORRECT);

	return 0;
}

void grade(char answers1[], char stu_answers1[], int NUM_QUESTIONS, int MIN_CORRECT)
{
	//cout << "max: " << NUM_QUESTIONS;
	int correctAnswers = 0;

	//Check the student's replies against the correct answers
	for (int i = 0; i < NUM_QUESTIONS; i++) {
		if (answers1[i] == stu_answers1[i])
			correctAnswers++;
	}
	//Did they pass or fail?
	if (correctAnswers >= MIN_CORRECT)
	{
		cout << "\nCongratulations!\n"
			<< "You have passed the exam!\n\n";
	}
	else
	{
		cout << "\nSorry, you have not passed the exam!\n\n";
	}

	//Display a list of the questions that were incorrectly answered.
	cout << "The list below shows the question numbers of the incorrectly";
	cout << " answered questions.\n";
	for (int i = 0; i < NUM_QUESTIONS; i++)
	{
		if (answers1[i] != stu_answers1[i])
			cout << "Question # " << i << " is incorrect." << endl;
	}

	//Display the number of correct and incorrect answers provided by the student.
	cout << "\nCorrect Answers = " << correctAnswers << endl;
	cout << "Incorrect Answers = " << NUM_QUESTIONS - correctAnswers << endl;
}
Hello morganniie,

See what you can do with this:
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
85
86
87
88
#include <iostream>
#include <cctype>

using namespace std;  // <--- Best not to use.

void grade(char[], char[], int, int);

int main()
{
    const int NUM_QUESTIONS = 10;
    const int MIN_CORRECT = 8;
    constexpr int MAX_TRIES{ 3 };
    char correctAnswers[NUM_QUESTIONS] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };

    char studentAnswers[NUM_QUESTIONS];
    int numTries{};

    //Loop for users answers
    for (int replies = 0; replies < NUM_QUESTIONS; replies++)
    {
        if (numTries == 3) break;

        cout << "Please enter your answers: "
            << (replies + 1) << ": ";
        cin >> studentAnswers[replies];

        studentAnswers[replies] = static_cast<char>(std::toupper(static_cast<unsigned char>(studentAnswers[replies])));

        //Validation of users answers
        //while (studentAnswers[replies] != 'A' && studentAnswers[replies] != 'B' && studentAnswers[replies] != 'C' && studentAnswers[replies] != 'D')
        while ((studentAnswers[replies] < 'A' || studentAnswers[replies] > 'D') && numTries < MAX_TRIES)
        {
            cout << "\n     You must enter A, B, C, or D\n\n";
            
            numTries++;

            cout << "Please enter your answers: " << (replies + 1) << ": ";
            cin >> studentAnswers[replies];
        }
    }

    if (numTries<MAX_TRIES)   
    {
        grade(correctAnswers, studentAnswers, NUM_QUESTIONS, MIN_CORRECT);
    }
    else
    {
        std::cout << "\n     Your good bye message!\n";
    }

    return 0;
}

void grade(char answers1[], char stu_answers1[], int NUM_QUESTIONS, int MIN_CORRECT)
{
    //cout << "max: " << NUM_QUESTIONS;
    int correctAnswers = 0;

    //Check the student's replies against the correct answers
    for (int i = 0; i < NUM_QUESTIONS; i++)
    {
        if (answers1[i] == stu_answers1[i])
            correctAnswers++;
    }
    //Did they pass or fail?
    if (correctAnswers >= MIN_CORRECT)
    {
        cout << "\nCongratulations!\n"
            << "You have passed the exam!\n\n";
    }
    else
    {
        cout << "\nSorry, you have not passed the exam!\n\n";
    }

    //Display a list of the questions that were incorrectly answered.
    cout << "The list below shows the question numbers of the incorrectly";
    cout << " answered questions.\n";
    for (int i = 0; i < NUM_QUESTIONS; i++)
    {
        if (answers1[i] != stu_answers1[i])
            cout << "Question # " << i << " is incorrect." << endl;
    }

    //Display the number of correct and incorrect answers provided by the student.
    cout << "\nCorrect Answers = " << correctAnswers << endl;
    cout << "Incorrect Answers = " << NUM_QUESTIONS - correctAnswers << endl;
}

Only worked with what is in "main". Have not tested the rest yet.

Andy
Perhaps:

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
#include <iostream>
#include <string>
#include <cctype>

void grade(const char[], const char[], unsigned, unsigned);

char getAns(const std::string& prm, unsigned maxAttpts) {
	char ans {};

	do {
		std::cout << prm;
		std::cin >> ans;
		ans = static_cast<char>(std::toupper(static_cast<unsigned char>(ans)));
	} while ((ans < 'A' || ans > 'D') && --maxAttpts && (std::cout << "Invalid answer (A - D)\n"));

	if (maxAttpts == 0) {
		std::cout << "Invalid answer. Maximum attempts now reached\n";
		return {};
	}

	return ans;
}

int main()
{
	constexpr unsigned NUM_QUESTIONS {10};
	constexpr unsigned MIN_CORRECT {8};
	constexpr unsigned MAX_ATTMPTS {3};
	constexpr char correctAnswers[NUM_QUESTIONS] {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D'};
	char studentAnswers[NUM_QUESTIONS] {};

	for (unsigned replies = 0; replies < NUM_QUESTIONS; ++replies) {
		studentAnswers[replies] = getAns("Please enter your answers: " + std::to_string(replies + 1) + ": ", MAX_ATTMPTS);

		if (studentAnswers[replies] == char {})
			return (std::cout << "Goodbye!\n"), 1;
	}

	grade(correctAnswers, studentAnswers, NUM_QUESTIONS, MIN_CORRECT);
}

void grade(const char answers1[], const char stu_answers1[], unsigned NUM_QUESTIONS, unsigned MIN_CORRECT)
{
	unsigned correctAnswers {};

	for (unsigned i = 0; i < NUM_QUESTIONS; ++i)
		correctAnswers += answers1[i] == stu_answers1[i];

	if (correctAnswers >= MIN_CORRECT)
		std::cout << "\nCongratulations!\n" << "You have passed the exam!\n\n";
	else
		std::cout << "\nSorry, you have not passed the exam!\n\n";

	if (correctAnswers != NUM_QUESTIONS) {
		std::cout << "The list below shows the question numbers of the incorrectly";
		std::cout << " answered questions.\n\n";

		for (unsigned i = 0; i < NUM_QUESTIONS; ++i)
			if (answers1[i] != stu_answers1[i])
				std::cout << "Question # " << i + 1 << " is incorrect.\n";
	}

	std::cout << "\nCorrect Answers = " << correctAnswers << '\n';
	std::cout << "Incorrect Answers = " << NUM_QUESTIONS - correctAnswers << '\n';
}

Last edited on
Topic archived. No new replies allowed.