
April 11th, 2006, 12:28 AM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 9
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 
|