Kod: Označi sve
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//prototypes of functions
void openAnswerKeyFile(ifstream&);
// put into array
void readAnswerKeyFile(string answerArray[], ifstream&);
void openStudentFile(ifstream&);
//void openOutputFile(ofstream&);
void readStudentAnswers(string studentAnswersArray[], ifstream&);
int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]);
int main() {
// creates object answerFile of type ifstream
string answers[25];
string studentAnswersArray[27];
bool missedQuestions[25] = {false};
ifstream answerFile;
openAnswerKeyFile(answerFile);
ifstream studentAnswers;
openStudentFile(studentAnswers);
//ofstream outputFile;
//openOutputFile(outputFile);
readAnswerKeyFile(answers, answerFile);
readStudentAnswers(studentAnswersArray, studentAnswers);
//string studentName=studentAnswersArray[1];
//cout << "student name " << studentName;
int numberOfWrong= compare(answers, studentAnswersArray, missedQuestions);
cout << "Number of wrong is " << numberOfWrong;
//outputFile.close();
return 0;
}
// opens answer file
void openAnswerKeyFile(ifstream& answerFile) {
// filenames are declared as an array because
// ifstream class method open requires char as parameter
char answerFilename[] = "Answer Key 1.txt";
char answerFilename2[] = "Answer Key 2.txt";
char answerFilename3[] = "Answer Key 3.txt";
string userInput = "";
while (userInput != answerFilename && userInput != answerFilename2 && userInput != answerFilename3) {
cout << "Enter answer key filename: " << endl;
getline(cin, userInput);
}
// take from user number of file. Number is located at index 11
answerFile.open(answerFilename);
}
void openStudentFile(ifstream& studentAnswers) {
// filenames are declared as an array because
// ifstream class method open requires char as parameter
char studentAnswerFilename[] = "Student Answers 1.txt";
char studentAnswerFilename2[] = "Student Answers 2.txt";
char studentAnswerFilename3[] = "Student Answers 3.txt";
string userInput = "";
while (userInput != studentAnswerFilename && userInput != studentAnswerFilename2 && userInput != studentAnswerFilename3) {
cout << "Enter student answers filename: " << endl;
getline(cin, userInput);
}
// takes from user number of file. Number is located at index 16
studentAnswers.open(studentAnswerFilename);
}
void openOutputFile(ofstream& outputFile) {
char outputFilename[] = "";
cout << "Enter output filename (max 25 characters): " << endl;
//takes input from user
cin.getline(outputFilename, 25);
// open file
outputFile.open(outputFilename);
if (!outputFile.is_open()) {
cout << "Cant open file";
}
}
void readAnswerKeyFile(string answerArray[], ifstream& answerFile) {
string line;
short i = 0;
while (answerFile.good()) {
getline(answerFile, line);
answerArray[i] = line;
i++;
}
answerFile.close();
}
void readStudentAnswers(string studentAnswersArray[], ifstream& studentAnswers) {
string line;
short i = 0;
while (studentAnswers.good()) {
getline(studentAnswers, line);
studentAnswersArray[i] = line;
i++;
}
studentAnswers.close();
}
int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]) {
short numberOfWrong=0;
for (short i = 0, j = 2; i < 25, j < 27; i++, j++) {
if (answers[i] != studentAnswersArray[j]) {
missedQuestions[i] = true;
cout << answers[i];
numberOfWrong++;
}
}
return numberOfWrong;
}Imaju 2 ulazne datoteke: jedna je s tacnim odgovorima imena Answer Key 1.txt i u njoj su odgovori poredani ovako:
A
C
D
A
Druga datoteka je Student Answers Key 1.txt i ovog oblika:
Identifikacijski broj studenta
Ime i prezime
A
C
B
A
Lista problema s kodom:
1. Zbog cega mi u liniji
if (answers != studentAnswersArray[j]) {
missedQuestions = true;
cout << answers;
izbaci samo 1 output? Tacno mi prebroji 4 pogresna odgovora ali linija cout << answers; ispise samo jedan.
2. U zadatku kaze da se netacni odgovori stave u bool niz i oznace sa true. Kakvu korist imam od toga?
3. Odgovori se trebaju provjeriti samo jednom.