Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
For this code, I was tasked to make a program that met all these requirements:

Part 1 :
Write a program that will quiz the user on at least 5 "C++" questions. The questions are
to be presented in multiple-choice form. Remember:
a. to ask the user to enter his/her name (maybe either first name or full name)
b. to use either number responses (1) or alpha responses (a)
c. to keep your questions, appropriate for school!
d. the number of users is unknown.
e. to develop a way to terminate the questionnaire.
f. to store the users' responses in a sequential file.

Part 2:
Create the answer file for your quiz questionnaire.

Part 3:
Write a program that will access your "data" file created in Part 1, and "score" the users'
responses against the "answer" file. Remember:
a. that the number of users is considered unknown.
b. to print out a display showing each user's name and score.

Any help would be greatly appreciated as I am a beginner in c++ and I am trying to learn all the mistakes I have made and the origin of them.

What I have tried:

#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; 
    int score = 0;
    char ans1,ans2,ans3,ans4,ans5,ans6; 
    ofstream fout;
};

void GetInfo(Components);
void Print(Components);
void PrintAnswer(Components);

int main()
{
    cout<<"Hello user,this code is meant to Quiz you on multiple choice questions and then grade your answers.";
    cout<<"When finished your Quiz enter in the Keyword \"finished\" to finish your quiz and this program";
    char Continue;
    do{
    Components variables;
    GetInfo(variables);
    Print(variables);
    PrintAnswer(variables);
    cout<<"Do you wish to take this quiz again? If so, please enter in y or Y otherwise press n.";
    cin>>Continue;
    }while(Continue == 'y' || Continue == 'Y');
}

void GetInfo(Components)
{
    variables.fout.open("Quiz.txt", ios::out);
    assert(!variables.fout.fail());
    cout<<"Okay user, please enter in your first and last name";
    getline(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."<<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"<<endl;
    cin>>variables.ans1;
    
    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"<<endl;
    cin>>variables.ans1;
    
    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"<<endl;
    cin>>variables.ans1;
    
    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"<<endl;
    cin>>variables.ans1;
    cout<<"............................................................................";
    
        if(ans1 == 'b' || ans1 == 'B'){
        variables.score++;
        }if(ans2 == 'd' || ans2 == 'D'){
        variables.score++;
        }if(ans3 == 'b' || ans3 == 'B'){
        variables.score++;
        }if(ans4 == 'c' || ans4 == 'C'){
        variables.score++;   
        }if(ans5 == 'b' || ans5 == 'B'){        {
        variables.score++;  
        }
        fout<<"Okay, "<<varibales.name<<" your total score is: "<<variables.score;
        
    variables.fout.close();
}

void Print(Components)
{
    ifstream fin;
    fin.open("Quiz.txt", ios::in);
    while(fin>>variables.name)
    {
        fin>>variables.score;
        if(variables.name != ""){
            cout<<"OKay, "<<variables.name<<" your score is "<<variables.score<<endl;
        }
    }
    cout<<"Your Quiz has now officially been completed!";
    fin.close();
}

void PrintAnswer(Components)
{
    variables.fout.open("AnswerSheet.txt", ios::in);
    cout<<"This is the file that has all the answers to your quiz and because you have finished it you can now see the answers."<<endl;
    fout<<"The answer to Question 1 is 'b'";
    fout<<"The answer to Question 2 is 'd'";
    fout<<"The answer to Question 3 is 'b'";
    fout<<"The answer to Question 4 is 'c'";
    fout<<"The answer to Question 5 is 'b'";
    variables.fout.close();
}
C++
<pre lang="c++">
Posted
Updated 7-Nov-20 9:32am
v6
Comments
Rick York 6-Nov-20 0:53am    
Time to break out the debugger. I certainly wouldn't expect anyone to debug that for you.

BTW - the toupper and tolower functions are very handy.
Member 14982907 7-Nov-20 13:43pm    
Yeah, I didn't think many people would want to debug this for me because it is very long, well for me it is anyway.
Richard MacCutchan 6-Nov-20 3:14am    
The questions, and their correct answer, should be read from a text file, not encoded in the program. So you need a method that reads the next question from the file, presents it to the user, and returns their answer, together with a flag indicating whether it is correct or not.
Richard Deeming 6-Nov-20 9:51am    
"Bumping" your question isn't going to make us change our minds and do your work for you.
Member 14982907 7-Nov-20 13:45pm    
I do not know what you mean by "bumping", but all I want is someone to help correct my code because my teacher could not explain it to me very well. I apologize If I came off in such a way that had offended you.

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
It is okay now, I have fixed my code and no longer need any help. Thank you, to all those who did though!

#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;
    int TotalScore = 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: ";
    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;
        cout<<"Okay, "<<variables.name<<" your total score is: "<<variables.score<<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;
        while(fin>>read)
        {
            cout<<read;
            cout<<"\nOkay, "<<variables.name<<", your score is "<<variables.TotalScore<<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();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900