I am hoping you can give me a little insight into my programming errors. This program should compare user entered answers to the correct answers. It should then output the total number of correct and incorrect answers, and also check for valid answers (A,B,C, or D).
Everything compiles fine, but the program doesn't function the way it should. Attached is my code. I specifically seem to be having an issue with taking characters into the array I defined. How do you set that up?
In your for loops you may want int i. In your cin>>answer[i] you can just leave it as cin>>answer. Also, in your if statement if (i != 'A' || 'B' || 'C' || 'D') you actually want to compare the answer the user input, either studenAnswer[i] or answer --> if (studentAnswer[i] !='A' || studentAnswer[i] !='B' || studentAnswer[i] !='C' || studentAnswer[i] !='D' ). In your next if statement change it to if (nonvalid == 3).
Thank you both so much for the replies! I just checked this at work, so I'll have to wait until I get home to make the changes. I definitely need to get better at this, but practice makes perfect!
OK, I applied the changes that you suggested, but my output still implies that the program is having trouble filling the array studentAnswers. If I put the whole string of correct answers in all at once, it says "A,B,C, and D are the only valid inputs. GOOD BYE. You have failed the test..."
If I try to put one answer in and hit enter, the skips to the end and gives me the same output as above. What am I missing?
I've added a cout<<studentAnswers at the bottom, but it only lets me fill the first 3 blocks or the array.
#include<iostream>
usingnamespace std;
int main()
{
constint SIZE = 10;
char studentAnswers[SIZE];//student answer
int matchingAnswers = 0; //variable to count correct answers
int nonvalid = 0; //variable to count invalid answers
char correctAnswers[] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D' };
cout << "Please enter your answers to your Driver's License Exam questions to " << endl;
cout << "the corresponding numbers. Please use only capital letters. " << endl;
for (int i = 0; i < SIZE; i++)
{
cout << i + 1<<" - ";
cin >> studentAnswers[i];
if (studentAnswers[i] != 'A' && studentAnswers[i] != 'B' && studentAnswers[i] != 'C' && studentAnswers[i] != 'D')
{
cout << "A,B,C, and D are the only valid inputs. Please try to input the answer again." << endl;
nonvalid++;
--i;
}
if (nonvalid == 3)
{
cout << "GOOD BYE" << endl;
break;
}
}
for (int i = 0; i < SIZE; i++)
{
if (studentAnswers[i] == correctAnswers[i])
matchingAnswers++;
}
if (matchingAnswers >= 8)
{
cout << "You have passed the exam." << endl;
}
else
cout << "You have failed the exam." << endl;
cout << "Total number of correct answers: " << matchingAnswers << endl;
cout << "Total number of incorrect answers: " << 10 - matchingAnswers << endl;
while (matchingAnswers >= 8);
return 0;
}
I'm sorry rjvc, I pasted the wrong code in the above post. I didn't notice it until now.
The || to && switch seemed to fix everything. I added the --i under nonvalid to keep the question count the same if the user entered in an invalid character into an answer. I also combined the correct answer output to clean it up a little.