Having trouble with averaging

This is a discussion on Having trouble with averaging within the C++ Programming forums, part of the General Programming Boards category; I am working on a program and it is not averaging properly. I cannot figure out how to fix this. ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    5

    Having trouble with averaging

    I am working on a program and it is not averaging properly. I cannot figure out how to fix this.

    In short, the program is set up to add in grades from 0 to 100; any grades over 100 are invalid, and a grade entry of 999 should stop the loop and average the grades.

    The problem I am having is that the program is counting the invalid grades, which is making the average calculation incorrect.

    Here is the program:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int x;
        double grade, totgrade, count;
        
        totgrade = 0;
        count = 0;
             cout << "Please enter grade:  ";
             cin  >> grade;
             do
             {   
                       if (grade > 100)
                       cout << "\nInvalid grade;  ";
                       if (grade <= 100)
                       totgrade = totgrade + grade;
                       cout << "\nPlease enter next grade:  ";
                       cin  >> grade;
                       count++;
                       }
                       while (grade != 999);
    
    
             do
             {
                        if (grade == 999)
                        cout << "The average of these grades is:  ";
                        cout << totgrade / count;
                        {
                             break;
                             }
                             } 
                             while (grade == 999);
        
    
                     
        cin >> x;        
        return 0;
        return EXIT_SUCCESS;
    }

    // TEST
    //Please enter grade: 1
    //
    //Please enter next grade: 99
    //
    //Please enter next grade: 0
    //
    //Please enter next grade: 100
    //
    //Please enter next grade: 111
    //
    //Invalid grade;
    //Please enter next grade: 999
    //The average of these grades is: 40


    Thanks in advance for any help you might be able to give me.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Either subtract 999 before you average, or break out of the loop before you add it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    The problem isn't occuring with the 999 entry.

    When I enter an invalid entry to test the program, it is adding the invalid entry (101 in this case) to the total number of entries. It is not adding the 101 to the total grades.

    As this part of the program is within the loop, I am unsure how to correct it.

    Any idea?

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    You are doing count++ regardless of whether the grade is valid or not.

  5. #5
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    count is incremented now matter what grade is. You only want to increase it when the grade entered is valid.

    Decrypt

    Edit: oops, someone else posted while I was typing.
    There is a difference between tedious and difficult.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    I tried moving the count++ up 4 lines so that it was before the totgrade statement but it didn't solve the problem. I also tried entering count = count -1 after the invalid if statement and that really messed up the calculation.

    I am new to C++ and don't know what else to try.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    I also tried reversing the order of the if statements...that didn't work either.

    Shouldn't there be a way to fix this within the loop?

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    21
    You want to include the count++ in the IF where the grade is less than 101. You would use brackets.

    http://www.cprogramming.com/tutorial/lesson2.html

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    5
    Brackets .....

    OMG I knew it would be something that simple!! You have no idea how much time I have spent on this.

    Thanks so much!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 03:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 11:58 AM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21