Code: Show/Hide abcdefabcdefabcdefab
1234567 abcdefabcdefabcdefab 9876543 abddefbbbdefcbcdefac 5554446 abcdefabcdefabcdef 4445556 abcdefabcdefabcdefabcd 3332221 abcdefghijklmnopqrst |
Code: Show/Hide 1234567 20 9876543 15 5554446 Too few answers. 4445556 Too many answers. |
Code: Show/Hide #include <iostream> #include <fstream> #include <string> using namespace std; int main () { ifstream key; ofstream results; string keyAnswer; string stuAnswer; int studentID = 9999999; int n = 0; int numCorrect = 0; key.open("exams.txt"); results.open("results.txt"); key >> keyAnswer; key >> studentID; key >> stuAnswer; while ( !key.eof() ) { // checking for invalid input values (letters other than a-f) while ( n < keyAnswer.length() ) { string typecheck = stuAnswer.substr(n, 1); if ( typecheck > "f" || typecheck < "a" ) { results << studentID << " "; results << "Invalid answers." << endl; } n++; } n = 0; // checking for answers with a lack of input values if ( stuAnswer.length() < keyAnswer.length() ) { results << studentID << " "; results << "Too few answers." << endl; } // checking for answers with too many input values else if ( stuAnswer.length() > keyAnswer.length() ) { results << studentID << " "; results << "Too many answers." << endl; } // the correct number of input values, but how many of them are correct? else if ( stuAnswer.length() == keyAnswer.length() ) { while ( n < keyAnswer.length() ) { string stuCheck = stuAnswer.substr(n, 1); string keyCheck = keyAnswer.substr(n, 1); if ( stuCheck == keyCheck ) numCorrect++; n++; } results << studentID << " "; results << numCorrect << endl; numCorrect = 0; } key >> studentID; key >> stuAnswer; } key.close(); results.close(); cout << "Answers checked. Please check output file for results.\n"; system("pause"); return 0; } |
Code: Show/Hide 1234567 20
9876543 15 5554446 Too few answers. 4445556 Too many answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 Invalid answers. 3332221 6 |
Code: Show/Hide key >> in; while(!key.oef()){ ........ ................. ......... key >> in;//on the last data entry, the instream stops at the end } |
Code: Show/Hide while(true){ key >> in; ............ ............... .......... if(key.eof) break; } |