Jul 22, 2022 at 3:04pm UTC
Hi, I am having a bit of a problem, my code reads from a .txt file with the data:
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF
DEF56278 TTFTFTTTFTFTFFTTFTTF
ABC42366 TTFTFTTTFTFTFFTTF
ABC42586 TTTTFTTT TFTFFFTF
The first line is a set of True/False correct answer, the second through fifth line consists of a student number followed by a blank space followed by a set of student answers, there are three marking principles. Correct, incorrect and blank.
For some reason when i print out each member as it is assigned to the correct answer char array it is as per the txt document but when i run the "testScore" function member [0] for correctAnswers is assigned as '6'. Please see code below (i have added some code after the function call to show what the array output is).
#include <iostream>
#include <fstream>
using namespace std;
const int STUDENT_NUMBER_LGTH = 8;
const int NUMBER_QUESTIONS = 20;
const int NUMBER_STUDENTS = 4;
const int ANSWER_CORRECT = 2;
const int ANSWER_INCORRECT = -1;
const int ANSWER_BLANK = 0;
void getData(ifstream& inputFile, char correctAns[], char studentNo[][STUDENT_NUMBER_LGTH], char studentAns[][NUMBER_QUESTIONS]);
void testScore(char correctAns[], char studentAns[][NUMBER_QUESTIONS], int finScore[]);
int main()
{
char correctAnswers[NUMBER_QUESTIONS];
char studentNumber[NUMBER_STUDENTS][STUDENT_NUMBER_LGTH];
char studentAnswers[NUMBER_STUDENTS][NUMBER_QUESTIONS];
int finalScore[NUMBER_STUDENTS];
ifstream inFile;
inFile.open("Ex2_Data.txt");
getData(inFile, correctAnswers, studentNumber, studentAnswers);
testScore(correctAnswers, studentAnswers, finalScore);
int student1;
int studentNumber1;
int questionNumber1;
int answerNumber1;
for (answerNumber1 = 0; answerNumber1 < NUMBER_QUESTIONS; answerNumber1++)
{
cout << correctAnswers[answerNumber1];
}
cout << endl;
for (student1 = 0; student1 < NUMBER_STUDENTS; student1++)
{
for (studentNumber1 = 0; studentNumber1 < STUDENT_NUMBER_LGTH; studentNumber1++)
{
cout << studentNumber[student1][studentNumber1];
}
cout << " ";
for (questionNumber1 = 0; questionNumber1 < NUMBER_QUESTIONS; questionNumber1++)
{
cout << studentAnswers[student1][questionNumber1];
}
}
inFile.close();
return 0;
}
void getData(ifstream& inputFile, char correctAns[], char studentNo[][STUDENT_NUMBER_LGTH], char studentAns[][NUMBER_QUESTIONS])
{
int student;
int studentNumberDigit;
int questionNumber;
int studentAnswerNumber;
char disregard;
for (questionNumber = 0; questionNumber < NUMBER_QUESTIONS; questionNumber++)
{
inputFile.get(correctAns[questionNumber]);
}
for (student = 0; student < NUMBER_STUDENTS; student++)
{
for (studentNumberDigit = 0; studentNumberDigit <= STUDENT_NUMBER_LGTH; studentNumberDigit++)
{
inputFile.get(studentNo[student][studentNumberDigit]);
}
inputFile.get(disregard);
for (studentAnswerNumber = 0; studentAnswerNumber < NUMBER_QUESTIONS; studentAnswerNumber++)
{
inputFile.get(studentAns[student][studentAnswerNumber]);
}
}
}
void testScore(char correctAns[], char studentAns[][NUMBER_QUESTIONS], int finScore[])
{
int student;
int questionNumber;
int finalScore;
for (student = 0; student < NUMBER_STUDENTS; student++)
{
finalScore = 0;
for (questionNumber = 0; questionNumber < NUMBER_QUESTIONS; questionNumber++)
{
if (studentAns[student][questionNumber] == correctAns[questionNumber])
{
finalScore += ANSWER_CORRECT;
}
else if (studentAns[student][questionNumber] == ' ')
{
finalScore += ANSWER_BLANK;
}
else
{
finalScore += ANSWER_INCORRECT;
}
}
}
}
Jul 22, 2022 at 4:35pm UTC
When posting code, please use code tags so that the code is readable!
[code]
formatted code goes here
[/code]
You're not dealing with student answers that have less answers than the number of questions. Consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <iostream>
#include <fstream>
#include <iomanip>
const int STUDENT_NUMBER_LGTH { 8 };
const int NUMBER_QUESTIONS { 20 };
const int NUMBER_STUDENTS { 4 };
const int ANSWER_CORRECT { 2 };
const int ANSWER_INCORRECT { -1 };
const int ANSWER_BLANK {};
void getData(std::ifstream& inputFile, char correctAns[], char studentNo[][STUDENT_NUMBER_LGTH], char studentAns[][NUMBER_QUESTIONS]);
void testScore(char correctAns[], char studentAns[][NUMBER_QUESTIONS], int finScore[]);
int main() {
char correctAnswers[NUMBER_QUESTIONS] {};
char studentNumber[NUMBER_STUDENTS][STUDENT_NUMBER_LGTH] {};
char studentAnswers[NUMBER_STUDENTS][NUMBER_QUESTIONS] {};
int finalScore[NUMBER_STUDENTS] {};
std::ifstream inFile("Ex2_Data.txt" );
if (!inFile)
return (std::cout << "Cannot open file\n" ), 1;
getData(inFile, correctAnswers, studentNumber, studentAnswers);
testScore(correctAnswers, studentAnswers, finalScore);
for (int answerNumber1 {}; answerNumber1 < NUMBER_QUESTIONS; ++answerNumber1)
std::cout << correctAnswers[answerNumber1];
std::cout << '\n' ;
for (int student1 {}; student1 < NUMBER_STUDENTS; ++student1) {
for (int studentNumber1 {}; studentNumber1 < STUDENT_NUMBER_LGTH; ++studentNumber1)
std::cout << studentNumber[student1][studentNumber1];
std::cout << ' ' ;
for (int questionNumber1 {}; questionNumber1 < NUMBER_QUESTIONS; ++questionNumber1)
std::cout << studentAnswers[student1][questionNumber1];
std::cout << ' ' << finalScore[student1] << '\n' ;
}
}
void getData(std::ifstream& inputFile, char correctAns[], char studentNo[][STUDENT_NUMBER_LGTH], char studentAns[][NUMBER_QUESTIONS]) {
for (int questionNumber {}; questionNumber < NUMBER_QUESTIONS; ++questionNumber)
inputFile.get(correctAns[questionNumber]);
inputFile >> std::ws;
for (int student {}; student < NUMBER_STUDENTS; ++student) {
for (int studentNumberDigit {}; studentNumberDigit < STUDENT_NUMBER_LGTH; ++studentNumberDigit)
inputFile.get(studentNo[student][studentNumberDigit]);
inputFile >> std::ws;
for (int studentAnswerNumber {}; studentAnswerNumber < NUMBER_QUESTIONS && inputFile.peek() != '\n' ; ++studentAnswerNumber)
inputFile.get(studentAns[student][studentAnswerNumber]);
inputFile >> std::ws;
}
}
void testScore(char correctAns[], char studentAns[][NUMBER_QUESTIONS], int finScore[]) {
for (int student {}; student < NUMBER_STUDENTS; ++student) {
int finalScore {};
for (int questionNumber {}; questionNumber < NUMBER_QUESTIONS; ++questionNumber)
if (studentAns[student][questionNumber] == correctAns[questionNumber])
finalScore += ANSWER_CORRECT;
else if (studentAns[student][questionNumber] == ' ' )
finalScore += ANSWER_BLANK;
else
finalScore += ANSWER_INCORRECT;
finScore[student] = finalScore;
}
}
TTFTFTTTFTFTFFTTFTTF
ABC54102 T FTFTFTTTFTTFTTF TF 27
DEF56278 TTFTFTTTFTFTFFTTFTTF 40
ABC42366 TTFTFTTTFTFTFFTTF 31
ABC42586 TTTTFTTT TFTFFFTF 23
PS Have you come to std::string yet? Using this rather than char arrays makes things much easier...
Last edited on Jul 22, 2022 at 4:41pm UTC