Click here to Skip to main content
14,843,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For some reason, my getline line won't work and my PintScore Function will not work.
Any help would be appreciated as I want to fix and understand why it does not work.



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,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();
}
Posted
Updated 7-Nov-20 13:13pm

Quote:
For some reason, my getline line won't work and my PintScore Function will not work.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
Member 14982907 7-Nov-20 19:18pm
   
Oh yes, I have heard of debuggers but never knew how to use them. Thank you for the help, Patrice T!
Does this code even compile? I ask because it doesn't look like it will to me. See this : getline (string) - C++ Reference[^]
   
Comments
Member 14982907 7-Nov-20 19:17pm
   
Without the getline statemnet it does compile I knew it was wong but its what my teacher taught me, hes not a very good teacher which makes it hard to learn anything, but there are people like you on this site that help alot.
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!
   
Comments
Member 14982907 7-Nov-20 19:20pm
   
":laugh 😂" yes I know that know and the help is very much appretiated OriginalGriff, you are always able to analyze my code and give useful feedback that helps me with my work. Thanks for that yeo.
OriginalGriff 8-Nov-20 2:11am
   
You're welcome!

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