Hello, I've been programming with C++ for only several weeks now, but I have come across a practice problem that has given me trouble.

The objectives: collect data from a student including: his major (CIS or Math), last name, number of credits completed, GPA, and tuition paid.

Then calculate and print: Average GPA,
total tuition paid,
average tuition paid,
average number of credits taken by all students,
name of student with highest GPA along with GPA,
and name of Math student with highest GPA along with GPA.

I'm not supposed to code arrays. This is practice that is similar to an exam I'll have soon.

I've attempted the first four calculations below; where is my code incorrect for these steps? Also, I need help on what code to use to display the highest GPA from the data. Any help on this work is appreciated.

This is what I have so far:

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

int main()
{
    int major;
    string lastname;
    float credits;
    float totalcredits =0;
    float averagecredits;
    float gpa;
    float totalgpa =0;
    float averagegpa;
    float tuition;
    float totaltuition =0;
    float averagetuition;
    float maxgpa;
    char doagain = 'y';
    int count;
    
    count = 0;
    while (doagain == 'y');
    {
    cout << "Enter your major (0=CIS, 1=Math): ";
    cin >> major;
    cout << "Enter your last name: ";
    cin >> lastname;
    cout << "Enter the number of credits completed: ";
    cin >> credits;
    cout << "Enter your gpa: ";
    cin >> gpa;
    cout << "Enter your tuition: ";
    cin >> tuition;
    totalcredits = totalcredits + credits;
    totaltuition = totaltuition + tuition;
    totalgpa = totalgpa + gpa;
    cout << "Do you want to continue y/n";
    cin >> doagain;
    count++;
    
    }
    cout << "The Total Tuition is: " << totaltuition <<endl;
    
    if (count > 0)
    {
    averagetuition = totaltuition/count;
    cout << "The Average Tuition is: " << averagetuition <<endl;
}
    if (count > 0)
    {averagegpa = totalgpa/count;
    cout << "The Average gpa is: " << averagegpa <<endl;
}
    if (count > 0)
    {averagecredits = totalcredits/count;
    cout << "The Average number of credits taken is: " <<averagecredits <<endl;
}
    
return 0;
}
Ancient Dragon commented: Thanks for using code tags on your very first post :) +24

Recommended Answers

All 4 Replies

Well in your question. You said that you need to use arrays. But You are not using any in your program. The best approach would be to analyse the question and think how you can use a index to get the required result and also where you will need arrays.

Apart from that, if you already are familiar with structs I prefer that you use them.

I made a mistake in the description. I'm not using arrays in this code.

I've attempted the first four calculations below; where is my code incorrect for these steps?

...
    while (doagain == 'y'); // remove this ;
    {
    cout << "Enter your major (0=CIS, 1=Math): ";
    cin >> major;
    cout << "Enter your last name: ";
    cin >> lastname;
    cout << "Enter the number of credits completed: ";
    cin >> credits;
    cout << "Enter your gpa: ";
    cin >> gpa;
    cout << "Enter your tuition: ";
    cin >> tuition;
    totalcredits = totalcredits + credits;
    totaltuition = totaltuition + tuition;
    totalgpa = totalgpa + gpa;
    cout << "Do you want to continue y/n";
    cin >> doagain;
    count++;
    
    }
...
}

Just a small mistake. Remove the ';' as I said above.

At this moment your program repeats the while loop over and over again, since the condition is true and the statement does not change this condition (well, you statement is actually empty due that ';').

If you remove the ';', then it will work fine.

commented: Very good catch. +0

Just a small mistake. Remove the ';' as I said above.

At this moment your program repeats the while loop over and over again, since the condition is true and the statement does not change this condition (well, you statement is actually empty due that ';').

If you remove the ';', then it will work fine.

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.