calculating students average

hi! i am doing this for a school project and i need help with the output. this is what the output is suppose to look like but i don't know how to get their. please help me!

this is the sample output that my program is suppose to look like:

Enter Quiz #1 ===>> 87
Enter Quiz #2 ===>> 92

Enter Homework #1 ===>> 100
Enter Homework #2 ===>> 93
Enter Homework #3 ===>> 98
Enter Homework #4 ===>> 88
Enter Homework #5 ===>> 100

Enter Test #1 ===>> 90
Enter Test #2 ===>> 87
Enter Test #3 ===>> 92

Enter your name ==>> Bob Jones


Bob Jones’s grades

Quiz Grades --> 87, 92
Homework Grades-->100, 93, 98, 88, 100
Test Grades --> 90, 87, 92


Quiz Average
------------
89.50


Home work Average
-----------------
95.80


Test Average
------------
89.67


Nine weeks average
-------------------
91


(the averages are supposed to be next to each other not underneath each other)





and then this is my program that looks nothing like the sample:


#include <iostream>
#include <stdio.h>
#include <string>
#include <iomanip>
using namespace std;

int main()

{

/*********************Enter Data Section*****************/

int Quiz1;
int Quiz2;
float QuizAverage;

int Homework1;
int Homework2;
int Homework3;
int Homework4;
int Homework5;
float HomeworkAverage;

int Test1;
int Test2;
int Test3;
float TestAverage;

string Name;

/*********************Process Data Section*****************/

cout << "Enter Quiz #1 ===>> ";
cin >> Quiz1;
cout << "Enter Quiz #2 ===>> ";
cin >> Quiz2;
cout << endl;
cout << "Enter Homework #1 ===>> ";
cin >> Homework1;
cout << "Enter Homework #2 ===>> ";
cin >> Homework2;
cout << "Enter Homework #3 ===>> ";
cin >> Homework3;
cout << "Enter Homework #4 ===>> ";
cin >> Homework4;
cout << "Enter Homework #5 ===>> ";
cin >> Homework5;
cout << endl;
cout << "Enter Test #1 ===>> ";
cin >> Test1;
cout << "Enter Test #2 ===>> ";
cin >> Test2;
cout << "Enter Test #3 ===>> ";
cin >> Test3;
cout << endl;
cout << "Enter Your Name ===>> ";
getline(cin, Name);
getline(cin, Name);
cout << endl;
cout << endl;
cout << "John Smith's grades" << endl;
cout << "Quiz Grades ===>> ";
cout << Quiz1 << "," << Quiz2 << endl;
cout << "Homework Grade ===>> ";
cout << Homework1 << "," << Homework2 << "," << Homework3 << "," << Homework4 << "," << Homework5 << endl;
cout << "Test Grades ===>> ";
cout << Test1 << "," << Test2 << "," << Test3 << endl;
cout << "QuizAverage" << setw(14) << "homeworkAverage" << setw(14) << "TestAverage";
cout << "--------------";

/*********************Display Data Section*****************/

QuizAverage = float(Quiz1+Quiz2)/2;
HomeworkAverage = float(Homework1+Homework2+Homework3+Homework4+Homework5)/5;
TestAverage = float(Test1+Test2+Test3)/3;

system("pause");
}
You are very nearly there. Just a couple of comments. The important one is the sequence in which things take place. It should be:

• Input
• Process
• Output

Regarding the getline, you had
1
2
3
cout << "Enter Your Name ===>> ";
getline(cin, Name);
getline(cin, Name);

That is understandable. You have the getline twice because it doesn't work properly. The reason why? After the previous cin >> input, the user presses the enter key, which leaves a trailing newline character '\n' remaining in the input buffer. A more usual way to remove it is to use cin.ignore(). In the code below I use an value of 1000 which will read and discard up to 1000 characters, or until a newline is found.

And after all that, your code doesn't even use the name but outputs John Smith instead ;)

I already mentioned the sequence of events. I renamed some of the sections to make it clearer what each was doing. Use whatever names you wish, but keep things in this order.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>  
#include <iomanip>
#include <string>

using namespace std; 

int main()
{
    /********************* Declare Variables Section***************/

    int   Quiz1;
    int   Quiz2;

    string Name;

    /********************* Input Data Section *********************/

    cout << "Enter Quiz #1 ===>> ";
    cin  >> Quiz1;
    
    cout << "Enter Quiz #2 ===>> ";
    cin  >> Quiz2;
    cout << endl;
    
    cin.ignore(1000, '\n'); // discard until end of line
    
    
    cout << "Enter Your Name ===>> ";
    getline(cin, Name);
    cout << endl << endl;

    /********************* Process Section ************************/

    float QuizAverage = (Quiz1 + Quiz2)/2.0;
    
    
    /********************* Output results Section *****************/

    cout << fixed << setprecision(2);
    
    cout << Name << "'s grades" << endl;
    cout << "Quiz Grades -->> ";
    cout << Quiz1 << ", " << Quiz2 << endl;
        
    cout << "\n\n";
        
    cout << "Quiz Average\n"
            "------------\n"
         << QuizAverage << "\n\n";
         
    system("pause");
}


Output:
Enter Quiz #1 ===>> 87
Enter Quiz #2 ===>> 92

Enter Your Name ===>> Fred Bloggs


Fred Bloggs's grades
Quiz Grades -->> 87, 92


Quiz Average
------------
89.50

Last edited on
Topic archived. No new replies allowed.