Ok, so I have most of the code working but I've been up for over 24 hours and I just cannot seem to get the test score to come out. Here's my code, hopefully someone with fresh eyes can spot my error, I know it has to be something simple (it always is).

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	string filename;
	ifstream inFile, powFile;
	const int SIZE = 20;
	char firstArray[SIZE];
	char secondArray[SIZE];
	int missed, avg, total, count;

	missed = 0;
	avg = 0;
	total = 0;

	inFile.open("c:\\correctanswers.txt");
	powFile.open("c:\\studentanswers.txt");

	for (count = 0; count < SIZE; count++)

		inFile >> firstArray[count];

	for (count = 0; count < SIZE; count++)

		powFile >> secondArray[count];
	
	
	for (count = 0; count < SIZE; count++)
	{
		if (firstArray[count] != secondArray[count])
		{
			cout << "Missed question #" << count;
			cout << "  Students answer: " << secondArray[count];
			cout << "  Correct answer: " << firstArray[count] << endl;
			missed++;
		}
		else;
		
	}
	
	total = SIZE - missed;
	cout << "Number of correct answers: " << total << endl;

	
	avg = total / SIZE;         //right in here i think
	cout << fixed << showpoint << setprecision(2);
	cout << "Percentage answered correctly: " << avg << "%" << endl;

	if (avg >= 70)
		cout << "The student passed the exam." << endl;
	else
		cout << "The student failed the exam." << endl;

	return 0;
}

total, SIZE, and avg are all ints. Change avg to a double and cast either total or SIZE (or both) to a double. double avg = (double)total/SIZE; Even though avg is a double, the result of int/int division is in integer and therefore truncated.

ah thanks, knew it was something simple i just wasn't seeing.

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.