Home > Software engineering >  Multiple File handling C , Variable scoring issue
Multiple File handling C , Variable scoring issue

Time:02-20

**

[Project Description PICTURE] - 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;
    }
}

After running the code it keeps the same score as the previous user and then adds it onto the next user's score. I want it so that after every time the code loops again the variables will be reset or something like that where I wont have this scoring issue

[Desired Routput of User Scores][2]

This is the result after I run the code more than once, see how the scores double because it is saving the previous user file value or something like that **

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

**

CodePudding user response:

Resseting your variables is the solution to your problem. Look at the following line for example:

s1  ;

Here you can see you are adding 1 to s1, which is ok, but the value of s1 matters at that point. During the first iteration, s1 = 0 (before the above line of code). But if the answer was correct, in the second iteration s1 = 1 (before the above line of code), and then the above line of code will set the value of s1 to 2.

So you can do either of the following:

1: Reset the variables:

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

..as such:

    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');

Or declare your variables inside the do-while loop as such:

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

Either of the above options will fix your issue.

  • Related