I made this program but I need to figure out how to exit the program
if the user does not input A,B,C,D, after three attempts. I'm new at building
programs and I would be grateful for some help!

#include <iostream>

using namespace std;

char studentAnswers(int);

int main()
{

int i;
int correct = 0;
int incorrect = 0;
char *correctAnswers = "BDAACABACD"; // answers
char grade[10]; // student answer array declaration

for(i = 0; i < 10; i++) //input answers
{
grade = studentAnswers(i);
}

for(i = 0; i < 10; i++)
{
if(grade == correctAnswers)
{
correct++; //correct answer counter
}
else
{
incorrect++; //incorrect answer counter
}
}

if (correct >= 8)
{
cout << "\nCongratulations!\n";
cout << "You have passed exam.\n";
cout << "Total number of correct answers: < " << correct << " >\n";
cout << "Total number of incorrect answers: < " << incorrect << " >\n";
}
else
{
cout << "Sorry. You have not passed the exam.\n";
cout << "Total number of correct answers: < " << correct << " >\n";
cout << "Total number of incorrect answers: < " << incorrect << " >\n";
}

return 0;
}


char studentAnswers(int i)
{

char choice;

cout << "Please enter your choice for Question " << i+1 << endl;
cin >> choice;
choice = toupper(choice);

while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;
}

return choice;
}

You weren't very clear, and that isn't very easy to read, but i think you're building a sentinel loop.

while (choice != 'A' && choice != 'B'&& choice != 'C' && choice != 'D')
{
cout << "You must enter A, B, C, or D.\n";
cin >> choice;

}

There's a few members in iostream that help with this.

while (cin.rdbuf()->in_avail() > 1 || cin.fail() || (cChar != 'a' && cChar != 'b' /*(etc)*/) )
{
     cout << "Invalid input\nEnter char: ";
     cin.clear();
     cin.ignore(cin.rdbuf()->in_avail)()).get(c);
}

I'm not all that sure that this will compile, but it outlines the general syntax of a forced input type/range

This article has been dead for over six months. Start a new discussion instead.