0

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

Edited by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by stripe
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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.