incorrect output

This is a discussion on incorrect output within the C++ Programming forums, part of the General Programming Boards category; I am just trying to write a program where a user enters a grade and then when -1 is entered ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    incorrect output

    I am just trying to write a program where a user enters a grade and then when -1 is entered the program outputs the average grade and exits. The code I have written compiles and works, but it subtracts 1 from the average. Confused as to why this is happening:

    //grades
    #include <iostream.h>

    int main()
    {
    int count = 0;
    int total = 0;
    int average;
    int grades = 0;
    while(grades != -1)
    {
    cout << "Please enter a grade(enter -1 to quit): \n";
    cin >> grades;
    total += grades;
    count++;}
    count--;
    average = total / count;
    cout << "Class average is: " << average << endl;
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Because if you input -1 here...
    cin >> grades;

    You still add it here....
    total += grades;

    Of course its going to be off by -1

    > count--;
    Perhaps total++
    to fix your erroneous total as well.

    if ( grades == -1 ) break;
    would save all this fudgery
    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
    Aug 2001
    Posts
    155
    Try this instead of your while loop. If it corrects the problem I think you can figure out what went wrong in your solution. It is an important problem to be aware of as it can rear it's ugly head in many different scenarios.

    cout << "Please enter a grade(enter -1 to quit): \n";
    cin >> grades;
    while(grades != -1)
    {
    total += grades;
    count++;
    cout << "Please enter a grade(enter -1 to quit): \n";
    cin >> grades;
    }

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Smile thanks

    thanks for your help... it works fine now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String incorrect for output command target
    By DarkAlex in forum C++ Programming
    Replies: 16
    Last Post: 08-19-2008, 10:32 PM
  2. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  3. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 07:46 PM
  4. String output, sometimes incorrect
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2002, 08:46 AM
  5. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 09:11 AM

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