0

So I am working on this program, and i have it mostly complete. I am just having trouble with running the loop again. If i run the loop again, it messes up by displaying both of the questions in the function at the same time.

And after the program is finished, i need to display all of the iterations at the same time.

// This program will gather the information for the quizzes that have been taken in a course to date 
and // output the information into a report on the screen.


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// Function Prototypes
void getQuiz(string&, int&, int&);


int main()
{
    char grade;
    char ans = 'Y';
    string name;
    int quiz_number = 0;
    int score;
    int poss_score;
    int counter = 1;
    double percentage;

    // Explain what the program will do
    cout << "This program will ask the user to enter the name of a quiz, the score aquired, and the 
total possible score.\n";
    cout << "It will display a report that will show the quiz number, the name, the points, total, 
percentage, and the letter grade." << endl << endl;

    do {
        // Display count
        cout << counter;

        cout << endl << endl;

        // Call function
        getQuiz(name, score, poss_score);

        // Accumulate the number of times the loop is run
        counter++;

        cout << "Would you like to add another quiz? (Y) or (N)" << endl;
        cin >> ans;

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

    cout << endl << endl;

    // Calculate the percentage
    percentage = score * 100 / poss_score;

    // Get the letter grade
    if (percentage <= 100 && percentage >= 90)
        grade = 'A';
    else if (percentage <= 89 && percentage >= 80)
        grade = 'B';
    else if (percentage <= 79 && percentage >= 70)
        grade = 'C';
    else if (percentage <= 69 && percentage >= 60)
        grade = 'D';
    else if (percentage <= 59)
        grade = 'F';

    // Display the results in a table
    cout << setw(5) << " Quiz" << setw(20) << " Title" << setw(20) << " Points" << setw(10) << " 
Total" << setw(10) << " Percent" << setw(10) << " Grade" << endl << endl;
    cout << setw(5) << counter << setw(20) << name << setw(20) << score << setw(10) << poss_score << 
setw(10) << percentage << setw(10) << grade << endl;

    return 0;
}

void getQuiz(string& name, int& score, int& poss_score)
{
    // Get the name of the quiz
    cout << "\nWhat is the name of the quiz? " << endl;
    getline(cin, name);

    cout << "\nWhat was the score aquired? " << endl;
    cin >> score;

    cout << "\nWhat was the total possible score acheivable? " << endl;
    cin >> poss_score;
}
  • You need to display the results in a table, but I don't see any tables in your program. All I see are variables that can only hold data about one quiz at a time. It's impossible to save information about multiple quizzes in these variables. You've probably been told about **arrays** or **vectors**. Instead of simple variables you need to use arrays to store all the information. That way you can store data about multiple quizzes and then print it all out at the end. Revise your notes about how to use arrays. Unfortunately this is probably going to be quite a large rewrite of the code above. – john Sep 26 '20 at 18:01
  • Also relevant for the part about displaying both questions at the same time is [this](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) well known problem that catches many newbies out. It's dangerous to mix `getline` and `>>` unless you know what you are doing. – john Sep 26 '20 at 18:10

0 Answers0