I am trying to create a program that compares two files that contain letters from A to E. Both files have 20 of them. Now im just stuck i feel that i could use if statements but i don't know how to go along with it. Any help will be greatly appreciated. Thanks

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

// Function prototypes
void readFile(char[], char[], int);
void compareAnswers(char[], char[], int, int &);
void displayTestResults(int, int);

int main()
{
   // Constant for the number of questions
   const int NUM_QUESTIONS = 20;
   
   // Number of questions missed.
   int numMissed;
   
   // Array to hold the correct answers
   char correct[NUM_QUESTIONS];
   
   // Array to hold the student's answers
   char student[NUM_QUESTIONS];
   
   // Read the correct answers.
   readFile("CorrectAnswers.txt", correct, NUM_QUESTIONS);
   
   // Read the student's answers.
   readFile("StudentAnswers.txt", student, NUM_QUESTIONS);
   
   // Compare the student's answers with the correct
   // answers.
   compareAnswers(student, correct, NUM_QUESTIONS, numMissed);
   
   // Display the test results.
   displayTestResults(numMissed, NUM_QUESTIONS);
   
   return 0;
}

//********************************************************
// The readFile function reads the contents of           *
// an answer file into an array.                         *
//********************************************************

void readFile(char filename[], char array[], int size)
{
   // Loop counter
   int count = 0;
   
   // File stream object.
   ifstream inFile;
   
   // Open the file.
   inFile.open(filename);
   
   // Read the contents of the file into the array.
   while (count < size && inFile >> array[count])
   {
      // Increment the counter.
      count++;
   }
   
   // Close the file.
   inFile.close();
}

//********************************************************
// The compareAnswers function compares the elements of  *
// the student array with the elements of the correct    *
// array. For each correct answer, a 'C' is stored in    *
// the results array. For each incorrect answer, an 'I'  *
// is stored in the results array.                       *
//********************************************************

void compareAnswers(char student[], char correct[],
                    int size, int &numMissed)
{
	int count;
	int correct;
	int wrong;

}

//********************************************************
// displayTestResults displays the test statistics.      *
//********************************************************

void displayTestResults(int numMissed, int numQuestions)
{
   
}

Edited 6 Years Ago by peter_budo: Please do not use CODE SNIPPED for posting question. Use FORUM THREAD.

Here's a compileable solution. It will grade a test of any length with any sort of single letters as answers. It should be somewhat fault tolerant, though I'll leave it to you to add in extra protection. Hope this helps:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

double gradeTest( const string& key, const string& studentTest ){
    double score = 0.0;
    for( unsigned int i=0; i<key.size(); i++ )
        score += key[i] == studentTest[i];
    score /= key.size();
    return score;
}

bool readTest( const string& fileName, string& answers ){
    char answer;
    ifstream fin( fileName.c_str() );
    if( !fin.good() )
        return false;
    fin >> answer;
    while( !fin.eof() ){
        answers.push_back( answer );
        fin >> answer;
    }
    return true;
}

int main()
{
    string key, test0;
    readTest( "key.txt", key );
    readTest( "test0.txt", test0 );
    double grade = gradeTest( key, test0 );
    cout << "grade: " << grade << endl;
    return 0;
}
Comments
DT, PLEASE stop doing homework for others. Give them code that POINTS to an answer, not THE answer

delete lines 80 and 81 because those two variables are not needed.

Now all you have to add is a simple loop tocompare the letters of each string. If they are not then same then increment NumMissed counter.

i keep getting spam mail to my email address and its getting on my last nerve so I am asking a moderator to delete it or for people who help me get rid of it. Thanks for your help though with the line deletion for i just used a for loop and it works

Why??

I'm pretty convinced that I moronically did someone's homework for them, and they want the thread deleted so that they can use the code without an instructor finding out.

lol no its not that is there a way that i can put up a picture to show you how many emails i have on my iphone for this fourm

i keep getting spam mail to my email address and its getting on my last nerve so I am asking a moderator to delete it or for people who help me get rid of it. Thanks for your help though with the line deletion for i just used a for loop and it works

If that is from DaniWeb, it isn't spam. You have an option set in your PROFILE to send you e-mails whenever someone posts a response to your thread. Click on "Contro Panel" at the top of the page in the yellow bar and turn that option off.

lol no its not that is there a way that i can put up a picture to show you how many emails i have on my iphone for this fourm

Is there a way I can put up a picture to show you how to edit your email notification options?

Oh, yeah, there is...

Attachments options.jpg 60.25 KB

please delete thread

Sorry, can't do that. No rules were broken, so I'm not deleting it :)
Now please stop flagging it.

This article has been dead for over six months. Start a new discussion instead.