hi, i tried to create a program that takes in 8 inputs in an array, then grade the user and see if they passed or not (6 out of 8 or higher is a pass), but my program always tells me that i passed even if i get 0. other than that it works fine as instructed.
#include <iostream>
using namespace std;
int grading(int correct, bool pass);
//declares the void function
int main()
{ //define all variables and arrays
char answer[8] = {'a', 'c', 'd', 'b', 'a', 'b', 'a', 'b'};
char input[8];
int mark[8];
int correct=0, incorrect=0, pos=0;
bool pass=true, send;
for(int i=1; i <= 8; i++) //for loop that takes in an answer for each question then determines if it is correct or incorrect
{
do //do loop that makes sure the user inputs a, b, c, or d and no other letter
{
cout <<"Enter a, b, c, or d for question#" << i << ": "; //user's input
cin >> input[pos];
if ((input[pos] != 'a') && (input[pos] != 'b') && (input[pos] != 'c') && (input[pos] != 'd'))
cout <<"Please enter only a, b, c, or d! \n";
}while((input[pos] != 'a') && (input[pos] != 'b') && (input[pos] != 'c') && (input[pos] != 'd'));
if (input[pos] == answer[pos]) //determines if the input was the correct answer and stores it in a counter
{
correct += 1;
mark[pos] = 1;
}
else //determines if the input was an incorrect answer and stores it in a counter
{
incorrect += 1;
mark[pos] = 0;
}
pos += 1; //counter to keep track of the position in the array for the input answers
}
send = grading(correct, pass);
if(send = true) //if statement to print if the user passed or failed
cout <<"You have passed the exam!\n";
else if (send = false)
cout <<"You have failed the exam.\n";
//output the total number of correct and incorrect questions, and which questions were incorrect
cout <<"Your total number of correctly answered questions is: " << correct << "\n";
cout <<"Your total number of incorrectly answered questions is: " << incorrect << "\n";
cout <<"The questions that were incorrect were: " << mark[0] << " " << mark[1] << " " << mark[2] << " " << mark[3] << " " << mark[4] << " " << mark[5] << " " <<mark[6] << " " << mark[7] << "\n";
cout <<"(1 means a correct answer, 0 means incorrect)\n";
system ("pause");
return 0;
}
int grading(int correct, bool pass)
{ //function that determines and prints if you passed the exam or not
if (correct >= 6) //receives the amount of correct answers and calculates if you passed or not
pass = true;
else if (correct < 6)
pass = false;
return (pass);
}