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 April 10th, 2006, 04:26 PM
deemanva deemanva is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 6 deemanva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 34 sec
Reputation Power: 0
Need Help

I have to write a program that grade a test, it has read the answers from one ".dat" file and show the answers on another ".dat" file, i know i have to compare strings, does anyone else have any tips for me, any help would be appreciated .

Reply With Quote
  #2  
Old April 10th, 2006, 05:18 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
What do you mean you have to "show" the answers in another .DAT file? Would it be OK to simply keep the answer manual in that .DAT file, or do you have to hard-code the answers into the program?

If it were me, I would read the test answers (as provided by the student) in, compare them to the correct answer (hard code the answers in an array, or read them from a file), then produce an output file containing the students answer followed by an indictaion of whether it was right or wrong. If it's wrong, then profide the correct answer behind that.
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old April 10th, 2006, 06:18 PM
deemanva deemanva is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 6 deemanva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 34 sec
Reputation Power: 0
yes i have to hard code the answers, the program as a result of comparing the strings has to output the number of answers that were right.

Reply With Quote
  #4  
Old April 11th, 2006, 12:28 AM
Randall311 Randall311 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 9 Randall311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 0
solution

Quote:
Originally Posted by deemanva
yes i have to hard code the answers, the program as a result of comparing the strings has to output the number of answers that were right.


Try this code I wrote. You should really try to understand it for yourself, and not just copy and paste. You have to understand before you can learn. This will take a student's test file, and compare it with an answer key, then output the grading results to a seperate file. I wasn't 100% on what you were looking for. I'm sure this is more then enough help though.

Code:
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char **argv)
{
	// input files (answerKey.dat), (studentTest.dat)
	ifstream infile;
	ifstream student;
	// output file (finalScore.dat)
	ofstream outfile;

	// iterator for correct answers
	int correctCount = 0;

	// error checking for input and output files
	//make sure we can open the files
	infile.open("answerKey.dat", ios::in);
	if (!infile)
	{
		cout << "Unable to open the file " << infile << endl;
		return -1; //error
	}
	student.open("studentTest.dat", ios::in);
	if (!infile)
	{
		cout << "Unable to open the file " << student << endl;
		return -1; //error
	}
	outfile.open("finalScore.dat", ios::out);
	if (!outfile)
	{
		cout << "Unable to open the file " << outfile << endl;
		return -1; //error
	}

	// count the number of lines in the infile (use in break for outfile)
	// Forward declaired variables for counting lines in file
	char value[1024] = {0};
	unsigned int count = 0;
	FILE *file = fopen("studentTest.dat", "r");
	if(file == NULL)
	{
		//error
		return -1;
	}
	size_t read_count; // This stores the count of bytes read
	// Read 1024 bytes at a time
	while((read_count = fread(value, 1, 1024, file)) > 0) 
	{
		for(unsigned int i = 0; i < read_count; ++i)
		{
			// Increment count for each end of line character
			if(value[i] == '\n') 
				++count;
		}
	}
	// output number of lines
	cout << "The input file has " << (count + 1) <<" lines." << endl;
	// close the file for line counting
	fclose(file);

	// set linenum for line processing
	int linenum = 0;

	// build the body of the outfile
	while(!infile.eof())
	{

		// open the student's written answers
		while(!student.eof())
		{
			// declare student's answer line
			std::string studentLine;

			// get answer line
			std::getline(student, studentLine);
			std::string studentAns = studentLine;

			// declare answerKey line
			std::string ansLine;

			// get answerKey line
			std::getline(infile, ansLine);
			std::string answer = ansLine;

			if (ansLine.size() <= 1) { // break out because it's blank
				break;
			} else { // made it! now process the lines

				if (studentLine.size() <= 1) { // break out because it's blank
					break;
				} else { // made it! now process the lines

					// increment linenum for file processing
					linenum++;

					// compare the strings for appropriate answers
					if ((strcmp( answer.c_str(), studentAns.c_str())==0))
					{
						// got the correct answer
						correctCount++;
						cout << linenum << " CORRECT" << endl;
						outfile << linenum << " CORRECT" << endl;
					}
					else
					{
						// got the wrong answer
						cout << linenum << " WRONG" << endl;
						outfile << linenum << " WRONG" << endl;
					}

					// break out when we reach the end of file to avoid a stack error
					if (student.eof()) break;

				} // end of else

			} // end of inner while loop

			// break out when we reach the end of file to avoid a stack error
			if (infile.eof()) break;

		} // end of else
	} // end of outer while loop

	// output total number correct
	cout << "\nTotal number of correct answers: " << correctCount << endl;
	outfile << "\nTotal number of correct answers: " << correctCount << endl;

	cout << "End of file processing" << endl;

	// close the files that we used
	infile.close();
	student.close();
	outfile.close();
	return (0);
}


download the attached .dat files to try it out. I will answer your questions if you have any. Please try to enhance your knowledge of C++ with this. (Rename attachments to .dat extension and put in the same directory as the compiled program) You're welcome.

-Justin
Attached Files
File Type: txt answerKey.dat.txt (61 Bytes, 208 views)
File Type: txt studentTest.dat.txt (61 Bytes, 200 views)

Reply With Quote
  #5  
Old April 11th, 2006, 12:40 AM
Randall311 Randall311 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 9 Randall311 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 0
Cool

You could even figure out the student's final grade by dividing the number of correct answers into the total number of questions, in this case the same as number of lines.

Code:
double finalScore = ( ( (double)correctCount / (double)linenum ) * 100 );

Reply With Quote
  #6  
Old April 11th, 2006, 09:03 AM
deemanva deemanva is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 6 deemanva User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 34 sec
Reputation Power: 0
Thank everyone for the help

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need Help


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT