I have the following two programs in which I want someone with the knowledge to help me check and add some lines
CODE #1
code to see did it catch the first error?
code to see did it catch another error?
CODE #2
Is my sizing of MaxNum correct?
Is the Vector declaration correct?
Is the push_back syntax correct?
In which line was the first error detected?
In which line was the second error detected?
In which line was the third error detected?
Was the percentage correct accurate?
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
int main()
{
// create vectors
vector<char> correct;
vector<char> student;
// read files
ifstream correctFile, studentFile;
correctFile.open("CorrectAnswers.txt");
studentFile.open("StudentAnswers.txt");
for (int i = 0; i < 20; i++)
{
char c, s;
correctFile >> c;
studentFile >> s;
// insert to vector
correct.push_back(c);
student.push_back(s);
}
correctFile.close();
studentFile.close();
// display your name
string name = "Name";
cout << endl << name << endl;
// display header
cout.width(10); cout << left << "Question";
cout.width(10); cout << left << "Correct";
cout.width(10); cout << left << "Incorrect" << endl;
// display list of missed questions
int missed = 0;
int count = correct.size();
for (int i = 0; i < count; i++)
{
if (student[i] != correct[i])
{
cout.width(10); cout << left << (i+1);
cout.width(10); cout << left << correct[i];
cout.width(10); cout << left << student[i] << endl;
missed++;
}
}
cout << endl << "Missed Questions: " << missed << endl;
// calculate percentage
double percentage = ((count-(double)missed)/count)*100;
cout << "Correct Answers : " << percentage << "% " << endl;
}
The following files are already provided by you:
CorrectAnswers.txt
StudentAnswers.txt
Explanation:
Here is a sample output of both the programs above:
StudentIncorrectAnswers.png
The program performs the following tasks:
reads the correct answers and student answers
counts the missed questions
displays the missed questions
displays the correct percentage
Let me know if you have additional questions, requests, or issues encountered so that I can address your concerns right away.
I can give you a link to the updated code based on your concerns if you need it.
Thanks!
StudentIncorrectAnswers.png
there is no 'maxnum' that you asked about.
your vectors look fine but:
vector<char> correct(20); //as if array[20] -- if its a known, never changing size, this is ok to do
yes, push back looks good.
also, you don't need 2 char variables there. You can reuse 1 char variable to read, push back, read, push back, like that. not a big deal, just a note on being careful about excessive variables (clutter, if nothing else).
line 45 detects errors.
percentage looks correct, but you should use c++ casts in formal code (type) is a C cast, and I use them too, but its not c++ anymore. I would have cast the denominator, but as long as any of the terms are doubles it should resolve.
that isnt everything you asked but --- test it? Actually run your code where you know the answer it should give (% correct, etc) and see if it is working on a few example files. Even small code like this, spotting all the errors by hand is a chore, but all in all, it looks good, its probably either working or very nearly, and you did a good job as far as I can see.