Home > other >  Output does not include all input for my array
Output does not include all input for my array

Time:09-02

I have this program that is barely started:

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

class Grade 
{
    public:
    string studentID;
    int userChoice = 0;
    int size = 0;
    double* grades = new double[size]{0};
};

void ProgramGreeting(Grade &student);
void ProgramMenu(Grade& student);
string GetID(Grade& student);
int GetChoice(Grade& student);
void GetScores(Grade& student);

int main()
{
    Grade student;
    ProgramGreeting(student);
    ProgramMenu(student);
}

// Specification C1 - Program Greeting function
void ProgramGreeting(Grade &student)
{
    cout << "--------------------------------------------" << endl;
    cout << "Welcome to the GPA Analyzer! " << endl;
    cout << "By: Kate Rainey " << endl;
    cout << "Assignment Due Date: September 25th, 2022 " << endl;
    cout << "--------------------------------------------" << endl;
    GetID(student);
    cout << "For Student ID # " << student.studentID << endl;
}

void ProgramMenu(Grade &student)
{
    cout << "--------------------------------------------" << endl;
    cout << setw(25) << "Main Menu" << endl;
    cout << "1. Add Grade " << endl;
    cout << "2. Display All Grades " << endl;
    cout << "3. Process All Grades " << endl;
    cout << "4. Quit Program." << endl;
    cout << "--------------------------------------------" << endl;
    GetChoice(student);
}

string GetID(Grade &student)
{
    cout << "Enter the student's ID: ";
    cin >> student.studentID;
    if (student.studentID.length() != 8) {
        cout << "Student ID's contain 8 characters ";
        GetID(student);
    }

    return student.studentID;
}

int GetChoice(Grade &student)
{
    cout << "Enter your selection: ";
    cin >> student.userChoice;

    if (student.userChoice == 1) {
        GetScores(student);
    }
    else if (student.userChoice == 2)
    {
    }
    else if (student.userChoice == 2)
    {
    }
    else if (student.userChoice == 4)
    {
        exit(0);
    }
    else 
    {
        cout << "Please enter 1, 2, 3 or 4" << endl;
        GetChoice(student);
    }
}

void GetScores(Grade &student)
{
    int count = 0;
    double score = 0;
    cout << "How many test scores would you like to enter for ID# "
        << student.studentID << "? ";
    cin >> student.size;
    while (count != student.size) {
        cout << "Enter a grade: ";
        cin >> score;
        for (int i = 0; i < student.size; i  ) {
            student.grades[i] = score;
        }
        count  ;
    }

    for (int i = 0; i < student.size; i  ) {
        cout << student.grades[i] << " ";
    }   
}

I am trying to make sure my array is recording all test scores, but when I output the array in my GetScore function, each element in the array is the same or equal to the last score I entered. For example, if I choose 3 for size and then enter three values of 99.2 86.4 90.1, all three elements will read 90.1.

Why is this happening?

output here

CodePudding user response:

Your Grade class is allocating an array of 0 double elements (which is undefined behavior), and then your GetScores() function does not reallocate that array after asking the user how many scores they will enter, so you are writing the input values to invalid memory (which is also undefined behavior).

Even if you were managing the array's memory correctly (ie, by using std::vector instead of new[]), GetScores() is also running a for loop that writes the user's latest input value into every element of the array, instead of just writing it to the next available element. That is why your final output displays only the last value entered in every element. You need to get rid of that for loop completely.

Try something more like this instead (there are several other problems with your code, but I'll leave those as separate exercises for you to figure out):

class Grade 
{
    public:
        ...
        int size = 0;
        double* grades = nullptr;
};

...

void GetScores(Grade &student)
{
    int size = 0;
    double score = 0;
    cout << "How many test scores would you like to enter for ID# " << student.studentID << "? ";
    cin >> size;

    if (student.size != size) {
        delete[] student.grades;
        student.grades = new double[size]{0};
        student.size = size;
    }

    for(int i = 0; i < size;   i) {
        cout << "Enter a grade: ";
        cin >> score;
        student.grades[i] = score;
    }

    for (int i = 0; i < size;   i) {
        cout << student.grades[i] << " ";
    }
}

Alternatively:

#include <vector>
...

class Grade 
{
    public:
        ...
        vector<double> grades;
};

...

void GetScores(Grade &student)
{
    size_t size = 0;
    double score = 0;
    cout << "How many test scores would you like to enter for ID# " << student.studentID << "? ";
    cin >> size;

    student.grades.resize(size);
    for (size_t i = 0; i < size;   i) {
        cout << "Enter a grade: ";
        cin >> score;
        student.grades[i] = score;
    }

    /* alternatively:

    student.grades.clear();
    for (size_t i = 0; i < size;   i) {
        cout << "Enter a grade: ";
        cin >> score;
        student.grades.push_back(score);
    }

    */

    for (size_t i = 0; i < size;   i) {
        cout << student.grades[i] << " ";
    }
}

CodePudding user response:

I removed my for loop from my while loop.

void GetScores(Grade &student)
{
    int count = 0;
    double score = 0;
    cout << "How many test scores would you like to enter for ID# "
        << student.studentID << "? ";
    cin >> student.size;
    while (count != student.size) {
        cout << "Enter a grade: ";
        cin >> score;
        student.grades[count] = score;
        
        count  ;
    }

    for (int j = 0; j < student.size; j  ) {
        
        cout << student.grades[j] << " ";
    }
    

    
}
  • Related