Reading from a text file and then using input using a program

I want the program to read a text file with 20 separate characters that are on separate lines. I want these characters to be matched with the programmed letters in the program, where they will display incorrect, and complete incorrect. I have successfully built a program in which I can manually enter letters and will do what I wanted, but my knowledge about using files in code is limited. I read about vectors, but first I would like it to be simple and succeed before I try to learn something else. I tried to create some kind of code similar to what, in my opinion, should look like.

Now I do not get any errors ... How to get text from a file to a program? I have built some code that almost does this, but cannot determine the last steps to connect it. Is it possible for someone to help me steer me in the right direction. Thanks in advance. With this forum, I am learning much more than I ever thought.

#include <iostream> #include <conio.h> #include <cctype> #include <fstream> #include <string> using namespace std; void input(char[], int); //Function prototype void checkAnswers(char[], char[], int, int); int main() { const int NUM_QUESTIONS = 20; const int MIN_CORRECT = 15; int correctAnswers = 0; //Accumulator for number of correct answers int incorrectAnswers = 0; //Accumulator for number of incorrect answers char answers[NUM_QUESTIONS] = { 'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char stu_answers[NUM_QUESTIONS]; ifstream infile; infile.open("key.txt"); //Check for Error if (infile.fail()) { cerr << "Error Opening File" << endl; exit(1); } string s1; int count = 0; // Reads file to the end while (!infile.eof()) { infile >> s1 >> stu_answers[NUM_QUESTIONS]; count++; } cout << count << " Students Answers" << endl; checkAnswers(answers, stu_answers, NUM_QUESTIONS, MIN_CORRECT); system ("pause"); return 0; } void checkAnswers(char answers1[], char stu_answers1[], int NUM_QUESTIONS, int MIN_CORRECT) { cout << "max: " << NUM_QUESTIONS; int correctAnswers = 0; int incorrectAnswers = 0; int wrongAnswers[]= {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; int j = 0; //Check the student replies against the correct answers for (int i = 0; i < NUM_QUESTIONS; i++) { if (answers1[i] == stu_answers1[i]) correctAnswers++; else if (answers1[i] != stu_answers1[i]) { incorrectAnswers++; wrongAnswers[j] = i + 1; j++; } } //Did they pass or fail? if (correctAnswers >= MIN_CORRECT) { cout << "\nYou must have at least 15 correct to pass."; cout << "\nStudent passed the exam\n\n"; } else { cout << "\nYou must have at least 15 correct to pass."; cout <<"\nStudent failed the exam\n\n"; } //Display a list of the questions that were incorrectly answered. cout << "The list below shows the question numbers of the incorrectly"; cout << " answered questions.\n"; for (int i = 0; i < NUM_QUESTIONS; i++) { if (wrongAnswers[i] != 0) cout << "Question # " << wrongAnswers[i] << " is incorrect." << endl; } //Display the number of correct and incorrect answers provided by the student. cout << "\nCorrect Answers = " << correctAnswers << endl; cout << "Incorrect Answers = " << incorrectAnswers << endl; } 

A text file is just a notepad text file with the name "key" with the following:

 C D A A C A D C C D B C D A D C A A D A 
+4
source share
2 answers

I think you can read characters directly. I checked the code below. You can try.

 while (!infile.eof() && count < NUM_QUESTIONS) { infile >> stu_answers[count++]; } 

count is still the number of student responses.

+1
source

You can use the std::getline function to get one line at a time. It looks like this:

 std::string strLine; int nCount = 0; while (std::getline(infile, strLine)) { stu_answers[nCount++] = strLine.at(0); } 

Then nCount will be the response number.

+1
source

Source: https://habr.com/ru/post/1491991/


All Articles