C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 7th, 2009, 12:39 AM
j.collins j.collins is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 2 j.collins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 55 sec
Reputation Power: 0
General - Need help with what should be a simple program

Hi guys,

I am working on our last program for a basic C++ programming class. This is what the problem says:
---------------------------------------
The history teacher at your school needs help in grading a true/false test. The students' IDs and test answers are stored in a file. The first entry in the file contains answers to the test in the form:

TFFTFFTTTTFFTFTFTFTT

Every other entry in the file is the student ID, followed by a blank, followed by the student's responses. For example, the entry:

ABC54301 TFTFTFTT TFTFTFFTTFT

indicates that the student ID is ABC54301 and the answer to question 1 is True, the answer to question 2 is False, and so on. This student did not answer question 9. The exam has 20 questions, and the class has more than 150 students. Each correct answer is worth 2 points, each wrong answer gets 1 point deducted, and no answer gets 0 points. Write a program to process the test data. The output should be the student's ID, followed by the answers, followed by the test score, followed by the test grade. Assume the following grade scale: 90%-100%, A; 80%-89.99%, B.......
--------------------------------------

So you get the idea. I have got most of the program finished but it has a few bugs that I can't figure out. Here is my code:

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int numOfAnswers = 20;

// Adjust maxanswers to accomadate the blank space before the answers actually begin
const int maxAnswers = numOfAnswers + 1;

char getGrade(int score, int possiblePoints);

int main()
{
	char answers[maxAnswers];
	char stuResponse[maxAnswers];
	string stuName;
	int i;
	int score = 0;
	int possiblePoints = 0;

	ifstream inFile;
	inFile.open("Grades.txt");
	ofstream outFile;
	outFile.open("Output.txt");

	// Store the answers in a variable
	answers[0] = '\0';
	for (i = 1; i < maxAnswers; i++)
	{
		inFile.get(answers[i]);
	}

	// Pull the students name or id number from start of line
	inFile >> stuName;

	// Pull the students answers
	for (i = 0; i < maxAnswers; i++)
	{
		inFile.get(stuResponse[i]);
	}

	// Check students choices with the answers and adjust score accordingly

	for (i = 0; i < maxAnswers; i++)
	{
		if (answers[i] == stuResponse[i])
		{
			score = score + 2;
			possiblePoints = possiblePoints + 2;
		}
		else if (answers[i] = ' ')
		{
			score = score;
		}
		else if (answers[i] != stuResponse[i])
		{
			score = score - 1;
			possiblePoints = possiblePoints + 2;
		}
	}

	// Output the student's ID, followed by the answers,
	// followed by test score, followed by test grade
	outFile << "Student: " << stuName << endl;
	outFile << "Correct answers: ";
	for (i = 1; i < maxAnswers; i++)
	{
		outFile << answers[i];
	}
	outFile << "\nTest Score: " << score << endl;
	outFile << "Grade: ";

	outFile << score << " <- score | poss points -> " << possiblePoints << endl;

	int percent = score / possiblePoints;
	
	outFile << percent;

	return 0;
}

/*
char getGrade(int score, int possiblePoints)
{
	int percent = score / possiblePoints;
	
	if (percent >= 90)
		return 'A';
	else if (percent >= 80)
		return 'B';
	else if (percent >= 70)
		return 'C';
	else if (percent >= 60)
		return 'D';
	else
		return 'F';
}*/


I have added some things to track what is going on as the program runs as well as commented some stuff out to work on later once I get the rest figured out. My main problem right now is the space between the student id and their answers. It was taking slot 0 in the studentResponse array, which threw the if statements off by one so I simply added a space in the answers array, but I dont think my teacher wants me to do this. Is there another way?

When I try to output the correct answers in the output files it seems to only be putting the answers the student got right, which is weird because I only assign the answers once in the program so the array shouldnt change...

And last but not least score and possiblePoints always seem to be the same no matter what. Here is the sample input file I was using:

Code:
TFFTFFTTTTFFTFTFTFTT
ABC54301 TFTFTFTT TFTFTFFTTFT


If you guys can help me out any I would really appreciate it!

Reply With Quote
  #2  
Old May 7th, 2009, 12:09 PM
j.collins j.collins is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 2 j.collins User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 55 sec
Reputation Power: 0
Ok I've got it working for one person, now the problem is the answers seem to be getting discarded each time the loop runs. I've added some extra lines to show what the value of each variable is prior to each comparison. Here is my code:

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

const int maxAnswers = 20;

char getGrade(int percent);

int main()
{
	char answers[maxAnswers];
	char stuResponse[maxAnswers + 1];
	string stuName;
	int i;

	ifstream inFile;
	inFile.open("Grades.txt");
	ofstream outFile;
	outFile.open("Output.txt");

	// Store the answers in a variable
	for (i = 0; i < maxAnswers; i++)
	{
		inFile.get(answers[i]);
	}

	while (!inFile.eof())
	{
	int score = 0;
	double possiblePoints = 40.0;
	double percent = 0.0;

	// Pull the students name or id number from start of line
	inFile >> stuName;

	// Pull the space from start of answers
	inFile.get();

	// Pull the students answers
	for (i = 0; i < maxAnswers + 1; i++)
	{
		inFile.get(stuResponse[i]);
	}

	// Check students choices with the answers and adjust score accordingly

	for (i = 0; i < maxAnswers; i++)
	{
		outFile << "Correct answer: " << answers[i] << endl;
		outFile << "Student answer: " << stuResponse[i] << endl;
		outFile << "\n" << endl;
		if (answers[i] == stuResponse[i])
		{
			score = score + 2;
		}
		else if (answers[i] = ' ')
		{
			score = score;
		}
		else if (answers[i] != stuResponse[i])
		{
			score = score - 1;
		}
	}

	// Output the student's ID, followed by the answers,
	// followed by test score, followed by test grade
	outFile << "Student: " << stuName << endl;
	outFile << "Answers: ";
	for (i = 0; i < maxAnswers; i++)
	{
		outFile << stuResponse[i];
	}
	outFile << "\nTest Score: " << score << "/40" << endl;
	percent = (score / possiblePoints) * 100;
	outFile << "Percent: " << percent << "%" << endl;
	outFile << "Grade: " << getGrade(percent) << endl;
	outFile << "\n" << endl;
	}

	inFile.close();
	outFile.close();

	return 0;
}


char getGrade(int percent)
{	
	if (percent >= 90)
		return 'A';
	else if (percent >= 80)
		return 'B';
	else if (percent >= 70)
		return 'C';
	else if (percent >= 60)
		return 'D';
	else
		return 'F';
}


here is the input file

Code:
TFFTFFTTTTFFTFTFTFTT
ABC54301 TFFTFFTTTTTTFFTFFFTT
Jerome TFTFTTFTFTTFTFTFTTTT
Test TFFTFFTTTTFFTFTFTFTT
Test2 TFFTFFTTTTFFTFTFTFTT
Test3 TFFTFFTTTT FTFTFTFTT

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Need help with what should be a simple program


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap