Это мой код C++ по обработке файловой системы наряду с функциями и структурами
По какой-то причине моя линия getline не будет работать, и моя функция PintScore не будет работать.
Любая помощь будет оценена по достоинству, так как я хочу исправить и понять, почему это не работает.
Что я уже пробовал:
#include <iostream> //library for input output stream #include <fstream> //library for reading and writing files #include <assert.h> //for the assert function using namespace std; struct Components //Components variable has all the variables relating to the Component of the Quiz { string name,done; int score; char ans1,ans2,ans3,ans4,ans5,ans6; ofstream fout; }; void GetInfo(Components &variables); void PrintScore(Components &variables); void AnswerSheet(Components &variables); int main() { cout<<"Hello user,this code is meant to Quiz you on multiple choice questions and then grade your answers."<<endl; char Continue; do{ Components variables; GetInfo(variables); PrintScore(variables); AnswerSheet(variables); cout<<"............................................................................"; cout<<"\nDo you wish to take this quiz again? If so, please enter in y or Y otherwise press any other key: "; cin>>Continue; }while(Continue == 'y' || Continue == 'Y'); } void GetInfo(Components &variables) { variables.fout.open("Quiz.txt", ios::out); assert(!variables.fout.fail()); cout<<"Okay user, please enter in your first and last name: "; getline(variables.name); //cin>>variables.name; cout<<"............................................................................"<<endl; cout<<"1: When working with multiple files (at the same time), the stream variables"<<endl; cout<<"\na. must all be of the same type, such as all ifstream, or all ofstream."; cout<<"\nb. must each be named independently, such as fin1, fin2, or fout1, fout2."; cout<<"\nc. must all be named the same, such as all fin and/or fout."; cout<<"\nd. are not needed since multiple files are present."; cout<<"\nANSWER: "<<endl; cin>>variables.ans1; cout<<"............................................................................"; cout<<"\n2: The required header file that allows classes of ofstream and ifstream to become available is"<<endl; cout<<"\na. iostream"; cout<<"\nb. filestream"; cout<<"\nc. assert.h"; cout<<"\nand. fstream"; cout<<"\nANSWER: "<<endl; cin>>variables.ans2; cout<<"............................................................................"; cout<<"\n3: When creating a new file, if a file of the same name already exists,"<<endl; cout<<"\nthe system will inform you that that file name is already in use."; cout<<"\na. true"; cout<<"\nb. false"; cout<<"\nANSWER: "<<endl; cin>>variables.ans3; cout<<"............................................................................"; cout<<"\n4: In the statement: fin.open(\"myfile.dat\", ios::in); the ios::in is the"<<endl; cout<<"\na. stream variable name"; cout<<"\nb. name of the file"; cout<<"\nc. stream operation mode"; cout<<"\nd. name of the buffer"; cout<<"\nANSWER: "<<endl; cin>>variables.ans4; cout<<"............................................................................"; cout<<"\n5: What is the purpose of this line of code? Be specific."<<endl; cout<<"\nfout.open(\"name.dat\",ios::app);"; cout<<"\na. Open a brand new binary file."; cout<<"\nb. Append the file"; cout<<"\nc. ios::app is a new file"; cout<<"\nd. Delete the file"; cout<<"\nANSWER: "<<endl; cin>>variables.ans5; cout<<"............................................................................"; if(variables.ans1 == 'b' || variables.ans1 == 'B'){ variables.score++; }if(variables.ans2 == 'd' || variables.ans2 == 'D'){ variables.score++; }if(variables.ans3 == 'b' || variables.ans3 == 'B'){ variables.score++; }if(variables.ans4 == 'c' || variables.ans4 == 'C'){ variables.score++; }if(variables.ans5 == 'b' || variables.ans5 == 'B'){ variables.score++; } cout<<endl; variables.fout.close(); } void PrintScore(Components &variables) { ifstream fin; fin.open("Quiz.txt", ios::in); if(fin.fail()) { cout<<"Error, please check your code."; } else { string read; //int TotalScore = variables.score; while(fin>>read) { cout<<read; cout<<"\nOkay, "<<variables.name<<", your score is "<<variables.score<<"/5"<<endl; } } cout<<"Your Quiz has now officially been completed!"<<endl; cout<<"............................................................................"<<endl; fin.close(); } void AnswerSheet(Components &variables) { variables.fout.open("AnswerSheet.txt", ios::in); cout<<"\nThis is the file that has all the answers to your quiz and because you have finished it you can now see the answers."<<endl; cout<<"\nThe answer to Question 1 is 'b'"; cout<<"\nThe answer to Question 2 is 'd'"; cout<<"\nThe answer to Question 3 is 'b'"; cout<<"\nThe answer to Question 4 is 'c'"; cout<<"\nThe answer to Question 5 is 'b'"<<endl; variables.fout.close(); }