I have looked around the website using the search and have seen old post so I am posting a new question most likely to an old answer. I am trying to read data from a file into an array and then pass it on to a function. I also have another file that I am trying to read into an array and pass it to a function. I don't have this program completed and I am looking for some guidance to finish it.

The Assignment is as follows:
One of your professors has asked you to write a program to grade her final exams, which consist of only 20 multiple choice questions. Each question has one of four possible answers: A,B,C, or D. The file CorrectAnswers.txt, which is on the student CD, contains the correct answers for all of the questions, each answer written on a separate line. The first line contains the answer to the first question, the second line contains the answer to the second questions and so forth.

Write a program that reads the contents of the CorrectAnswers.txt file into a one-demensional char array, and then reads the contents of another file, containing the students asnwers, into a second char array. The student CD has a file named StudentAnswers.txt that you can use for testing purposes. The program should determine the number of questions that the student missed, and then display the following:
A: A list of questions missed by the student, showing the correct answer and the incorrect answer provided by the student for each missed question.

B: The total number of questions missed.

C: The percentage of questions answered correctly. ( Correctly answered questions divided by total number of questions)

D: If the percentage of correctly answered questions is 70% or great, the program should indicate that the student has passed the exam. Otherwise, it should state the student has failed the exam.

//This program grades student scores. This program will calculate the score and show the grade percentage.
//Show a list of questions missed, show the incorrect answer and the correct answer.
//The total number of questions missed. 
//The program will state if the student passed (grade above 70%) or if they did not.
//Programming Challange 7.12
#include <iostream>
#include <fstream>
using namespace std;

// Function prototypes
void getCorrectAnswers(char[], int); // Used to read the correct answers key
void getStudentAnswers(char[], int); // used to read the student answers
void compareAnswers(char[], int); // used to compare the answers and then display the missed quetions with the correct questions
void testGrade(float[] ,int); // used to grade the test based on the compared questions, 70% or greater to pass

int main()
{
	
	cout << "This program will read the correct test answers and the student answers.\n";
	cout << "First we will read the correct answers into the program...\n";
	getCorrectAnswers;
	cout << "Now reading the student answers file...\n";
	getStudentAnswers;
	cout << "Going to now show the missed questions with the correct answers.\n";
	compareAnswers;
	testGrade;
		
		
	system ("pause");
	return 0;
}
// This function reads the correct answers key.
void getCorrectAnswers(char[], int)
{
const int ARRAY_SIZE = 20;  // Array size
	int correctAnswers[ARRAY_SIZE];	 // Array with 20 correct answers
	int count;						 // Loop counter variable
	ifstream inputFile;			 // Input file stream object
	
	inputFile.open("C:\\Users\\sixcore\\Documents\\Visual Studio 2010\\Projects\\exam_grader\\CorrectAnswers.txt"); // Correct answers
	
	// Read the 20 correct answers from the file into the array.
	for (count = 0; count < ARRAY_SIZE; count++)
		inputFile >> correctAnswers[count];
	
	// Close the file.
	inputFile.close();

	return correctAnswers;
}

	//This function reads the student Answers.
	void getStudentAnswers(char[], int)
	{
	
	// This array reads the student answers to the array
	const int ARRAY_SIZE = 20; // Array Size
	int studentAnswers[ARRAY_SIZE]; // Array with 20 student answers
	int count;                        //Loop counter variable
	ifstream inputFile;

	inputFile.open ("C:\\Users\\sixcore\\Documents\\Visual Studio 2010\\Projects\\exam_grader\\StudentAnswers.txt"); // Student answers

	// Read the 20 student answers
	for (count = 0; count < ARRAY_SIZE; count++)
		inputFile >> studentAnswers[count];

	// Close the file.
	inputFile.close();

	return studentAnswers;
	}
	// This function compares the Correct Answers key to the student answers.
	void compareQuestions(char[], int)
	{
		const char SIZE = 1;
		char correctAnswers;
		char studentAnswers;
		char compareAnswers;
		bool arraysEqual = true;
		int count = 0;

		//Compare the student answers to the correct answers.
		while (arraysEqual && count < SIZE)
		{
			if (correctAnswers[count] != studentAnswers[count])
				arraysEqual = false;
			count ++;
		}
		 
			if (arraysEqual)
				cout << "The Correct Answers compared to the student answers.\n" << compareAnswers << endl;
			else
				return;
	}
	//This function grades the test answers
	// Having troule here with how to finish
	void testGrade(float[], int)
	{
		int grade;
		cout << "Going to now show the grade if it is higher than 70%.\n";
		if (grade >= 70)
			cout << "The student has passed.\n";
		else
			cout <<" The student did not pass.\n";
	}

What I would have done is almost the same but a little different.. I would loop through the arrays like you did and I would print the number of the missed question to the screen and the correct answer to it.. I would put a counting integer in the loop aswell so that it counts how many were missed. Next I'd also make a global integer that would grab how many were right and divide that by 20.

My attempt at outputting the missed questions and answers to them.. I'd then add a global variable to count how many were wrong..

int wrong = 0; // global variable.

		while (arraysEqual && count < SIZE)
		{
			if (correctAnswers[count] != studentAnswers[count])
				arraysEqual = false;
			count++;
                        wrong++;
			
			if(studenAnswers[count] == "\n")     //My attempt at detecting a missed answer which would probably be a newline character in the file.
			{
				cout<<"The Student Missed Question: #"<<count<<"\n";
				cout<<"The Correct Answer to the question is: "<<correctAnswers[count]<<"\n";
			}
		}

Iunno if this is what u want but this is how I'd to the grade thing..

void testGrade(float[], int)
	{
		int grade;
		cout << "Going to now show the grade if it is higher than 70%.\n";
		
		grade = (20 - wrong)/20;  //This gives all the correct ones then divides by the total amount of questions..
		grade *= 100; //This gives a percentage.. in other words: grade = grade * 100.. Now u can do the >=70% thing..
		
		if (grade >= 70)
			cout << "The student has passed.\n";
		else
			cout <<" The student did not pass.\n";
	}

Oh and just to answer the question you were asking: To pass a variable from an array to a function, you can use a pointer to the array and pass that to the function.. Or u can do it the cheap way and make the array global and just pass it directly to the function.

Edited 5 Years Ago by triumphost: n/a

Out of all that information above, not one question was asked, not one problem you have was mentioned. Since we aren't "The Psychic Programmers Network (R)" we have nothing to look for. Try adding details you want answered.

Sorry, please let me clarify... (My compilers is Microsoft Visual C++ 2010 express)
I am wondering how I can get this part of the code to return the elements read from the answer key:

void getCorrectAnswers(char[], int)
{
const int ARRAY_SIZE = 20;  // Array size
	int correctAnswers[ARRAY_SIZE];	 // Array with 20 correct answers
	int count;						 // Loop counter variable
	ifstream inputFile;			 // Input file stream object
	
	inputFile.open("C:\\Users\\sixcore\\Documents\\Visual Studio 2010\\Projects\\exam_grader\\CorrectAnswers.txt"); // Correct answers
	
	// Read the 20 correct answers from the file into the array.
	for (count = 0; count < ARRAY_SIZE; count++)
		inputFile >> correctAnswers[count];
	
	// Close the file.
	inputFile.close();

	return correctAnswers;
}

The error I am seeing in my compiler is that the return value type does not match the function type which is right. How can I change it to match and return the answer key back to the array to assist in comparing the answer key to the student answers.

I pose the same question on how to return the variables from the student answers file and return the elements in the studentAnswers.txt from this code as I get the same error on the return studentAnswers.

void getStudentAnswers(char[], int)
	{
	
	// This array reads the student answers to the array
	const int ARRAY_SIZE = 20; // Array Size
	int studentAnswers[ARRAY_SIZE]; // Array with 20 student answers
	int count;                        //Loop counter variable
	ifstream inputFile;

	inputFile.open ("C:\\Users\\sixcore\\Documents\\Visual Studio 2010\\Projects\\exam_grader\\StudentAnswers.txt"); // Student answers

	// Read the 20 student answers
	for (count = 0; count < ARRAY_SIZE; count++)
		inputFile >> studentAnswers[count];

	// Close the file.
	inputFile.close();

	return studentAnswers;
	}

I have also added the global counter called wrong = 0.

I think thats how u do it below.. U have to use pointers because the way your doing it, the array disappears when the function returns since the array was declared in the function.. which is why I'd use a global array.. anyway since the array disappears when the function returns, if u try to grab data from the array in any other function, it will be trying to get data from an address that doesn't exist..

That's the best that I could explain it.. I really don't know how to explain it well but thats sorta how I understand it..

int *getCorrectAnswers(int[]);
int *CAnswers = getCorrectAnswers(correctAnswers);
int *getCorrectAnswers(int correctAnswers[])
{
const int ARRAY_SIZE = 20;  // Array size
	correctAnswers[ARRAY_SIZE];	 // Array with 20 correct answers
	int count;						 // Loop counter variable
	ifstream inputFile;			 // Input file stream object
	
	inputFile.open("C:\\Users\\sixcore\\Documents\\Visual Studio 2010\\Projects\\exam_grader\\CorrectAnswers.txt"); // Correct answers
	
	// Read the 20 correct answers from the file into the array.
	for (count = 0; count < ARRAY_SIZE; count++)
		inputFile >> correctAnswers[count];
	
	// Close the file.
	inputFile.close();

	return correctAnswers;
}

Edited 5 Years Ago by triumphost: n/a

Since you can't pass a character array as a return value, pass the emptycorrectAnswers[] array into the function rather than defining it in the function.

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