Thread: Math related calculation error?

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    17

    Math related calculation error?

    Ok, so the issue I am now having with this program is that the average calculation is incorrect. However, the equation itself is so simple it seems unlikely that I could be so bone-headed as to mess that up. So, I am left thinking it has something to do with placement inside the if statements.

    Sample input/output: enter 1, 3, the break loop with control + z. This should give an average of 2 but instead gives an average of 2.33333.

    The other problem I have is when the user breaks the loop before entering a number. It gives the "out of range, ignored" error before the "No data was entered" error. The first error should not occur and only happens when control+Z is used before any numbers.

    Code:
    /*
    int main () 
    {
           double number, tot, counter, average, range, low, high;
           counter = 1.0;
           tot = 0.0;
           low = 100.0;
           high = 0.0;
           while (!cin.eof())
           {
               cin >> number;
               if ((number >= 0.0) && (number <= 100.0))
               {
                   counter = counter + 1.0;
                   tot = tot + number;
                   if (number < low)
                   {
                       low = number;
                   }
                   if (number > high)
                   {
                       high = number;
                   }
                }
               if (number < 0.0 || number > 100.0)
               {
                   cout << "out of range; ignored." << endl;
               }
           }
           if (counter == 0.0) 
           {
               cout << "No data was entered" << endl;
           }
           else 
           {
               average = tot / counter;
               cout << "The average is " << average << endl;
               range = high - low;
               cout << "The range is " << range << endl;
           }       
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    while (!cin.eof())
    This is your problem. Don't use eof() to control a loop. The reason is that the eof bit is not turned on until after you try to read from cin and the read fails because it gets the ctrl-z. If you look at your code, it will run through the loop properly the first two times, but then the third time when you try to read into number, the read fails. But that happens after you've already checked for eof(), so the loop continues using the same value that number had the last time through. So it's adding 1 + 3 + 3, which of course averages to 2.33.

    The fix is to use the result of the read to control your loop. Move the call to cin >> into the while control:
    Code:
    while (cin >> number)
    The return value of cin >> number evaluates to true if it successfully read in a number and false otherwise, so it will return false and break the loop when you enter ctrl-Z.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    You set counter to 1.0 before any numbers are entered, meaning it always thinks there is one number more than there actually is. Of course, that doesn't explain your output of 2.3333 but I confirmed that the faulty output of you is actually faulty :P.

    Also, counter shouldn't be a double, it should be an integer, as it is an integer.

    Also, comparing doubles with "==" is not correct. You should find the difference of the two numbers and makes sure the difference is small enough for that case.


    Edit: Ah, I missed what Daved said, so that makes it even more problematic.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Thanks for the help guys. Dave, that did the trick and all is now well.

    EVOE, I realized counter should have been an int but what do you mean about not using ++ with a double? My program was actually returning 1.#INF before it was suggested that I do that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM