Using While Loops??

Hello Fellow programmers,

Im currently learn c++ and for my assignment im asked to write a program that calculates the average of test scores(0-100). The program stores grades until the user enters -1. Once the user enters -1 then the program prints out the average of the grades prior to the -1. In addition the program also prints out the highest grade that the user input. Please help I dont quite know what im suppose to do!

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
 #include <iostream>
using namespace std;

int main() {

        int x = 0;
        int number;
        int total;

        while(x != -1)
        {
                cin >> number;
                total = total + number;
                x++;

        if(number == -1)
        {
                cout << "The average for all grades is: " <<((total + 1) / x) << endl;
        }

        }




return 0;
}
"hw4.cpp" 33L, 328C written                                                                                        
[pejo9770@venus hw]$ g++ hw4.cpp
[pejo9770@venus hw]$ ./a.out hw4.cpp
51
45
80
-1
The average for all grades is: 1049156
50
50
100
-1
The average for all grades is: 524602
You never initialize total so it just starts out with some random garbage value that you add to.

Your while loop is infinite because x can never be negative - I think you meant to use a different variable.

You should check that the number the user entered is not -1 before adding it to total.
I prefer to use a few extra variables just for clarity sake.
Giving your variables meaningful names instead of generic names like "x" is helpful
Initializing your variables and a few conditional funtions can work wonders.


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
#include <iostream>
using namespace std;

int main()
{

    int number, total =0;                           // Got rid of x
    int highgrade, avg, counter = 0;            //  Added three variables

    while(number > -1)
    {
        cout << "Please enter grade  ";
        cin >> number;
        if (number > -1)
        {
            counter++;
        }
        total = total + number;
        avg = total / counter;              // averages grades every time a new grade is entered

        if  (number>highgrade)
        {
            highgrade=number;             // checks for the highest number
        }
    }

    cout <<  "\nYou entered " << counter << " grades." << endl;   
    // Used to make sure 'counter' was accurate
    cout <<  "Your grades total " << total << endl;   
    // Used to ensure grades were totalled correctly
    cout << "\nThe highest grade entered is: " << highgrade << endl;
    cout << "\nThe average for all grades is: " << avg  << endl;

    return 0;
}

In the above code , you cant check highest number directly with previous one, the output seems not good.. try this suits for all.

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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{

    int number, total =0;                  
    int  avg, counter = 0;            
     std::vector<int> arr;
    do {
        cout << "Please enter grade  ";
        cin >> number;
        arr.push_back(number);
        total = total + number ;
        avg = total / counter;
         counter ++;
    }   while(number > -1);
    
       int highgrade = *max_element(arr.begin(), arr.end());
    
    cout <<  "\nYou entered " << counter << " grades." << endl;   
    cout <<  "Your grades total " << total << endl;   
    cout << "\nThe highest grade entered is: " << highgrade << endl;
    cout << "\nThe average for all grades is: " << avg  << endl;

    return 0;
}
Thank you guys, So much great insight and information. Specifically Pearlyman, I dont quite understand what effect does initializing the variable have on the programs. Im sorry if this is a newbie question but I really want to understand the concepts taught to me, not just ask for help anytime i encounter a problem. Thank you again!

When you write a code, sometimes it can get extremely large, spread out over many files.
When a function routine goes stupid on ya, you want to find your variables quickly.
If the Monster I call "Orc" is showing up with green eyes, and I think they should be black... It's easier to look for Orc.eyes than try to remember which variable x, y, and z controls...

Orc.hair_color or x
Orc.eyes or y
Orc.disposition or z

By using meaningful names, You can tell right away what that variable is used for.

Initializing a variable gives the variable a starting point. A beginning value. In your computer memory, somewhere in the dark corners of your CPU resides the variable a. It's a scary place until you are a very experience programmer (which I am not). In that memory location where 'a' lives is a value. It could be left over from Windows starting, or a game you were playing earlier. No one knows. It's likely to be different on your computer than it is on mine. Declaring it, reserves the right for you to use it, but it does not clear the value. That's your job. So we initialize our variable to give them a starting value. This is how we control the outcome of our code.

int a; is not the same as

int a; // this declares it
a=0; // this initializes it

You can also do it in one step... int a=0;

I hope that clears it up.



Thanks, really appreciate the insight!
Topic archived. No new replies allowed.