Home > Software design >  Console not printing expected output
Console not printing expected output

Time:11-24

I am trying to expand on previous code by implementing 2D-array's, however I keep getting issues with the console not outputting the correct values. The console is not taking in the right values when calculating the average and outputs 0 instead of the expected value. When running the code, the section where it would display the High and the Low scores would always display the first number that was typed in.

There are restrictions to work under.

  1. Adjust the logic to drop the high score and the low score and average the remaining three scores for each student. The student grade is based on the average of the 3 middle scores.

  2. All data is read in from the keyboard.

  3. Two global constants may be utilized: one for the number of students and one for the number of tests.

  4. Display in a table format the student's name, 5 test scores, average, and grade. Include a header in the table to label each column respectively.

  5. Use iomanip and setw() to format the output.

  6. Main should consist of variable declarations and function calls. This means the for loops to process the arrays resides in the functions, not in main.

  7. Has to follow the base code.

`

using namespace std;

const int SCORES = 5;
const int NUM_STUDENTS = 3;

int main()
{
    string name[NUM_STUDENTS];
    int test[NUM_STUDENTS][SCORES];
    char grade[NUM_STUDENTS];
    float avg{};
    int total = 0;
    int hiIndex{}, loIndex{};

    calcData(name, test, grade, total, hiIndex, loIndex, avg);

    //display results
    displayResults(name, test, grade, avg, loIndex, hiIndex);

    system("pause");
    return 0;
}

void calcData(string name[], int test[][SCORES], char grade[], int total, int hiIndex, int loIndex, float& avg)
{
    for (int counter = 0; counter < NUM_STUDENTS; counter  )
    {

        getInput(name, test, counter, total);
        cin.ignore();

        //find index of the highest score and lowest score
        findHiAndLow(test, hiIndex, loIndex, counter);

        //assign letter grade
        assignGrade(avg, grade, counter);

        //calculate the class average
        calcAvg(total - (test[counter][hiIndex]   test[counter][loIndex]), avg, SCORES - 2);

    }

}

void getInput(string arrOne[], int arrTwo[][SCORES], int size, int& t)
{
    //get student name
    cout << "Input the student name and press enter\n";
    getline(cin, arrOne[size]);

    for (int i = 0; i < SCORES; i  )
    {
        //get student test score 
        cout << "Input the score for the midterm test\n";
        cin >> arrTwo[size][i];

        //(accumulate scores) total of all scores
        t  = arrTwo[size][i];
    }

    cout << endl;
}

int findHiAndLow(int t[][SCORES], int& h, int& l, int row)
{
    for (int i = 0; i < SCORES; i  )
    {
        if (t[row][h] < t[row][i])
            h = row;
        if (t[row][l] > t[row][i])
            l = row;

    }
    return h, l;
}

float calcAvg(int t, float a, int size)
{
    a = static_cast<float>(t) / size;

    return a;
}

void displayResults(string n[], int t[][SCORES], char g[], float a, int low, int high)
{
    for (int counter = 0; counter < NUM_STUDENTS; counter  )
    {
        cout << left << setw(10) << n[counter] << ":";
        for (int i = 0; i < SCORES; i  )
        {
            cout << setw(10) << t[counter][i];
        }
        cout << endl;
    }

    cout << "\n\nThe class average for this test = " << a << endl << endl;
    for (int i = 0; i < NUM_STUDENTS; i  )
    {
        cout << n[i] << " your highest test score = " << t[i][high] << endl;
        cout << n[i] << " your lowest test score = " << t[i][low] << endl << endl;
    }

}

`

The expected outcome was for the program to take the average of the 3 middle scores that are left after dropping both the high and low scores from the initial 5 scores that are given. I have tried rearranging the values in both findHiandLow() and getInput(). I have tried having both for loops for getInput() within the function and have switched back to having one on the outside (within calcData()) to include the other functions, with the intent of having it loop for each student.

I wanted the console to print out the average of the three middle scores and not include the High and low, I was also expecting the console to print out the High and low scores for the student but it only prints the first score.

If my numbers were, for example, 12, 89, 45, 100, 23; The expectation would've been that it would drop the 12 and 100 and leave me with 89, 45, and 23. It would then take the average of those 3 numbers which in theory should result in 52.34 and result in an "F", however it prints out 0. and because the number that was first typed in was 12 the lowest and highest number would be listed as 12. It should have been 12 and 100 respectively.

CodePudding user response:

This is another case of the incredibly common newbie confusion over returning values from functions.

This is your function

float calcAvg(int t, float a, int size)
{
    a = static_cast<float>(t) / size;

    return a;
}

You are trying to calculate the average and return the result, but for some reason you have declared a as a parameter, instead of as a local variable. This is how it should look

float calcAvg(int t, int size)
{
    float a = static_cast<float>(t) / size;

    return a;
}

Once you see that you should see that it can be further simplified, eliminating a altogether

float calcAvg(int t, int size)
{
    return static_cast<float>(t) / size;
}

Now look at how you call calcAvg.

calcAvg(total - (test[counter][hiIndex]   test[counter][loIndex]), 
    avg, SCORES - 2);

you are calling the function, but doing nothing with the returned value. This is how it should look

avg = calcAvg(total - (test[counter][hiIndex]   test[counter][loIndex]), 
    SCORES - 2);

Now the return value of calcAvg is being assigned to the variable avg changing it's value. This is clearly what you intended. If you want to change the value of a variable using a functions return value the syntax is x = func(); not func(x).

Really not sure why this is such a stumbling block for newbies. The correct code has always seemed natural and straightforward to me. But, in any case, remember that parameters and return values are different things, with different syntax, and different purposes.

  • Related