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);
}
stephen84s commented: First post and used code tags :-) +7

Recommended Answers

All 3 Replies

Just observe the following code snippet:-

if(send = true) 
  cout <<"You have passed the exam!\n";
else if (send = false)
  cout <<"You have failed the exam.\n";

Watch both the conditions carefully you have used only a single "=" instead of "==" to test for equality.
BTW some compilers in this situation would complain of "Possible incorrect assignment".

Also there is no need for passing the second argument "pass" to your grading function, instead just declare a local variable in the "grading()" function and return its value.

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";

As I mentioned to the other poster of similar code, you've made a small, common mistake in here. Can you spot it yourself?

And, when test for true result fails (value is false), is there a need for the else if ( condition is false) test? Just use else - there cannot be any other possibility.

Lastly, the bool parameter you pass to the grading function does not convey any information into the function. You really are using it as a local variable in the function. So, drop the parameter and either declare a bool variable in the function, or simply return true or false at the appropriate places. And again, just an else in the function, something is either >= 6 or it's not.

gah..i keep overlooking that problem :( tnx :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.