Help with a c++ grading program?
-
You’ve been asked to write a program to grade a multiple choice exam. The exam has 20 questions, each answered with a little in the range of ‘a’ through ‘f’. The data are stored on a file(exams.dat) where the first line is the key consisting of a string of 20 characters. The remaining lines on the files are exam answers, and consist of a student ID number, a space, and a string of 20 characters. The program should read the key, then read each exam and output the ID number and score to file scores.dat. Erroneous input should result in an error message. For example, given the data: abcdefabcdefabcdefab 1234567 abcdefabcdefabcdefab 9876543 abddefbbbdefcbcdefac 5554446 abcdefabcdefabcdef 4445556 abcdefabcdefabcdefabcd 3332221 abcdefghijklmnopqrst The program should output on scores.dat: 1234567 20 9876543 15 5554446 Too few answers 4445556 Too many answers 3332221 Invalid answers Use functional decomposition to solve the problem and code the solution using functions as appropriate. Be sure to use proper formatting and appropriate comments in your code. The output should be neatly formatted, and the error messages should be informative #include <fstream> #include <iostream> #include <string> #include <iomanip> using namespace std; void readInput(string&, int [], string []); // read keys, ids, and answers void gradeExam(string, int [], string []); // grade each student's answer int main() { string key; // the key of the exam string answers[20]; // answers from students int id[20]; // ids of students ofstream outFile; // keeps the exam result readInput(key, id, answers); gradeExam(key, id, answers); return 0; } // read data from file void readInput( /* out */ string& key, /* out */ int id[], /* out */ string ans[]) { string filename; ifstream inFile; // contains the exam data to be processed int i = 0; cout << "Enter input file name: "; cin >> filename; inFile.open(filename.c_str()); inFile >> key; inFile >> id[i]; while (i) { inFile >> ans[i]; i++; } inFile.close(); } *****I figured out the readInput function and the gradeExam function. As for the gradeExam function I know how to use boolean values and substrings but we cant use them in this program. I need to know how to convert it into something else like while loops or if statements***** void gradeExam( /* in */ string key, /* in */ int id[], /* in */ string ans[], int numofstudent) { ofstream outFile; // output file to contain the grade report int pos = 1; int score = 0; int i = 0; // Ask for the output file name and open output file outFile.open("scores.dat"); // grade each answer and write the result to the file while(count < numofstc) { if(ans[i].length() < 20) outFile << fixed << setw(10) << id[i] << setw(10) << "Too few answers" << endl; else if(ans[i].length() > 20) outFile << fixed << setw(10) << id[i] << setw(10) << "Too many answers" << endl; else { pos = 1; bool tof = false; score = 0; while(posit < 21) { if(key.substr(pos, 1) == ans[i].substr(pos, 1)) score += 1; else if(ans[count].substr(pos, 1) != "a" && ans[i].substr(pos, 1) != "b" && ans[count].substr(pos, 1) != "c" && ans[i].substr(pos, 1) != "d" && ans[count].substr(pos, 1) != "e" && ans[i].substr(pos, 1) != "f") { outFile << fixed << setw(10) << id[i] << setw(10) << "Invalid answer" << endl; tof = true; break; } pos++; } if(!tof) outFile << fixed << setw(10) << id[i] << setw(10) << score << endl; } i++; } outFile.close(); }
-
Answer:
Do play with the functions. Main is working #include <fstream> #include <iostream> #include <string> #include <iomanip> using namespace std; void readkey(ifstream &A, string ); void readInput(ifstream &C, int &A, string B); // read keys, ids, and answers int gradeExam(string A, string B); // grade each student's answer int main() { string key; // the key of the exam string answers; // answers from students int id; // ids of students ifstream inFile("xx.dat"); ofstream outFile("out.dat"); // keeps the exam result //readkey(inFile,key); inFile>>key; cout<<key<<endl; while( !inFile.eof()){ //readInput(inFile, id, answers); inFile>>id>>answers; cout<<key<<"\t"<<answers<<endl; cout<<id<<"\t"<<gradeExam(key,answers)<<… outFile<<id<<"\t"<<gradeExam(key,answers… } system("PAUSE"); return 0; } void readkey(ifstream &A, string x){ A>>x; } void readInput(ifstream &A, int &id, string x){ A>>id>>x; } int gradeExam(string A, string B){ int c=0; for(int i=0;i<A.length();i++) c+=(A[i]==B[i]); return c; }
john at Yahoo! Answers Visit the source
Other answers
readinput is a forever loop. it never stops incrementing i. you have the wrong condition for stopping. .good() tells you if the stream is good or not, and .good() includes .eof()
Jim
Related Q & A:
- How to Convert a C++ Structure into C# Structure?Best solution by Stack Overflow
- How to run an executable from within a c program?Best solution by Stack Overflow
- Can someone help me find a study abroad program?Best solution by Yahoo! Answers
- Should I make a C&C cage for my guinea pigs?Best solution by Yahoo! Answers
- Does completing a social justice program in HS actually help your career prospects?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.