多文件处理 C++,变量评分问题

Multiple File handling C++, Variable scoring issue

**

[项目描述图片] - https://i.stack.imgur.com/bVA5F.png

**

#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;

int main()
{
string name;

//score variables
int s1 = 0,s2 = 0,s3 = 0,s4 = 0,s5 = 0;
//student answer variables
char q1,q2,q3,q4,q5;

//answer variables
char a1 = 'b';
char a2 = 'd';
char a3 = 'a';
char a4 = 'c';
char a5 = 'b';

//fstream variables 1 for each file
fstream file1,file2,file3;

char Continue;
do{
    cout<<"\t\t\t\t\t Gr 12 Computer Science File Handling C++ Quiz"<<endl;

    cout<<"\nOkay user, please enter in your first name: "<<endl;
    cin>>name;

    cout<<"\nHello "<<name<<", this Quiz has a total of 5 marks, each question is worth 1 marks. Good Luck!"<<endl;
    cout<<endl;

    file1.open("AnswerSheet.txt", ios::out);
        file1<<"-----"<<endl;
        file1<<"AnswerSheet"<<endl;
        file1<<"Q1: "<<a1<<endl;
        file1<<"Q2: "<<a2<<endl;
        file1<<"Q3: "<<a3<<endl;
        file1<<"Q4: "<<a4<<endl;
        file1<<"Q5: "<<a5<<endl;
        file1<<"-----"<<endl;
    file1.close();

    file2.open("Quiz.txt", ios::out);
        cout<<"\n-----"<<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>>q1;
        cout<<"-----"<<endl;

        cout<<"-----"<<endl;
        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<<"\nd. fstream";
        cout<<"\nANSWER: "<<endl;
        cin>>q2;
        cout<<"-----"<<endl;

        cout<<"-----"<<endl;
        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>>q3;
        cout<<"-----"<<endl;

        cout<<"-----"<<endl;
        cout<<"\n4: In the statement: obj2.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"<<endl;
        cout<<"\nANSWER: "<<endl;
        cin>>q4;
        cout<<"-----"<<endl;

        cout<<"-----"<<endl;
        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>>q5;
        cout<<"-----"<<endl;

        file2<<"-----"<<endl;
        file2<<name<<", Quiz answers"<<endl;
        file2<<"Q1: "<<q1<<endl;
        file2<<"Q2: "<<q2<<endl;
        file2<<"Q3: "<<q3<<endl;
        file2<<"Q4: "<<q4<<endl;
        file2<<"Q5: "<<q5<<endl;
        file2<<"-----"<<endl;

file2.close();

string read1,read2;

file1.open("AnswerSheet.txt", ios::in);
file2.open("Quiz.txt", ios::in);

    while(getline(file1,read1) && getline(file2,read2))
    {
            if(a1 == q1)
            s1++;
            if(a2 == q2)
            s2++;
            if(a3 == q3)
            s3++;
            if(a4 == q4)
            s4++;
            if(a5 == q5)
            s5++;

    file3.open("ScoreUser.txt", ios::out);

            int totalscore = (s1 + s2 + s3 + s4 + s5);

            file3<<"\n-----"<<endl;
            file3<<"Student name: "<<name<<", scores"<<endl;
            file3<<"Question 1 score: "<<s1<<endl;
            file3<<"Question 2 score: "<<s2<<endl;
            file3<<"Question 3 score: "<<s3<<endl;
            file3<<"Question 4 score: "<<s4<<endl;
            file3<<"Question 5 score: "<<s5<<endl;
            file3<<"total score: "<<totalscore<<"/5"<<endl;
            file3<<"-----"<<endl;

    file1.close();
    file2.close();
    file3.close();
    }

    file3.open("ScoreUser.txt", ios::in);
    string read3;
    while(getline(file3,read3))
    {
            cout<<read3<<endl;
    }
    file3.close();


    cout<<"\n Enter y to take quiz or enter n to terminate"<<endl;
    cin>>Continue;

    }while(Continue == 'y' || Continue == 'Y');

    file3.open("ScoreUser",ios::trunc);
    file3.close();

    if(Continue != 'y'|| Continue != 'Y')
    {
        cout<<"program terminated"<<endl;
    }
}

在 运行 编码后,它保持与前一个用户相同的分数,然后将其添加到下一个用户的分数中。我想要它,以便在每次代码再次循环后,变量将被重置或类似的东西,我不会有这个评分问题

[用户分数的期望输出][2]

这是我 运行 代码不止一次后的结果,看看分数是如何翻倍的,因为它保存了以前的用户文件值或类似的东西 **

问题 - https://i.stack.imgur.com/K2U4W.png Desired Output - https://i.stack.imgur.com/vcBv2.png

**

重置您的变量是您问题的解决方案。例如,查看以下行:

s1++;

在这里您可以看到您正在将 1 加到 s1,这没问题,但此时 s1 的值很重要。在第一次迭代期间,s1 = 0(在上面的代码行之前)。但是如果答案是正确的,在第二次迭代中s1 = 1(在上面这行代码之前),然后上面这行代码会将s1的值设置为2。

因此您可以执行以下任一操作:

1:重置变量:

s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0; // Add this line of code at the end of your do-while loop

..因此:

    cout << "\n Enter y to take quiz or enter n to terminate" << endl;
    cin >> Continue;

    s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0;
        
} while (Continue == 'y' || Continue == 'Y');

或者在 do-while 循环中这样声明你的变量:

//score variables
//int s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0;
//student answer variables
char q1, q2, q3, q4, q5;

//answer variables
char a1 = 'b';
char a2 = 'd';
char a3 = 'a';
char a4 = 'c';
char a5 = 'b';

//fstream variables 1 for each file
fstream file1, file2, file3;

char Continue;
do {
    int s1 = 0, s2 = 0, s3 = 0, s4 = 0, s5 = 0; // Declare variables here

    cout << "\t\t\t\t\t Gr 12 Computer Science File Handling C++ Quiz" << endl;

以上任一选项都可以解决您的问题。