Need Help

Okay so i'm in a c++ programming class and i need some help. I'm trying to take my file and make it so it compares two char arrays from files that i already have. I'm not looking for an answer just some hints and advice because i have no idea how to do this. My code looks like this.

This is my code:

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

int main()
{

const int arraysize = 20;

char array1[arraysize];

int i = 0;

int count = 0;

ifstream infile;

infile.open("CorrectAnswers.txt");

while (!infile.eof())
{
infile >> array1[i];
i++;
count++;
}


infile.close();

char array2[arraysize];

ifstream infile2("StudentAnswers.txt");
i = 0;
while (!infile2.eof())
{
infile2 >> array2[i];
i++;
}


infile2.close();


cout << "Question" << setw(12) << "Correct" << setw(12) << "Student" << endl;

cout << " #" << setw(12) << "Answers" << setw(12) << "Answers" << endl;

cout << "---------------------------------" << endl;

for(int i = 0; i < arraysize; i++)

cout << setw(8) << i+1 << setw(12) << array1[i] << setw(12) << array2[i] << endl;

return 0;
}
Last edited on
Line 11: arraysize should be const

Line 23,37: Do not loop on ! stream.eof(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


Line 46,56: array3 is not needed. Just use i+1.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
thank you for your help but what my code works for everything i needed it to work for before but now i need to make it so that it goes through and checks to see if the correct answers matches the student answers. If it doesn't it needs to display the ones that aren't correct. i'm not sure how to do that.
You simply need to iterate through the arrays comparing the student answers with the correct answers.

1
2
3
4
5
6
7
8
9
10
//  Compares the student's answers to the correct answers
//  Returns number of correct answers
int evaluate_answers (const char correct_ans[], const char student_ans[])
{   int correct = 0;

    for (int i=0; i<arraysize; i++)
        if (student_ans[i] == correct_ans[i])
            correct++;
    return correct;
}


BTW, your variable names for your arrays (array1, array2) suck. Your variable names give no useful information to the reader.
Topic archived. No new replies allowed.