Thread: Double returns 1.#INF

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

    Double returns 1.#INF

    I understand 1.#INF represents infinity and I am sure the issue is simple, but I cannot for the life of me figure out what is causing it. The issue is when trying to find the average. Everything seems like it is in its place.

    Code:
    /*
    CMPSC 101
    Section 01
    Fall Semester
    2010
    Justin Eckrote
    JAE5086
    */
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()  // Program determines what fruit you want.
    {
           double number, tot, counter, average, range, low, high;
           counter = 0.0;
           tot = 0.0;
           low = 100.0;
           high = 0.0;
           while (!cin.eof())
           {
               cin >> number;
               if ((number >= 0.0) && (number <= 100.0))
               {
                   if (number < low)
                   {
                       low = number;
                   }
                   if (number > high)
                   {
                       high = number;
                   }
                   counter = counter + 1.0;
                   tot = tot + number;
               }
               if ((number < 0.0) && (number > 100.0))
               {
                   cout << "out of range; ignored." << endl;
               }
           }
           if (counter = 1.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
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Examine your if statements for = where you should be using ==
    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.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    17
    Ok, I am pretty sure I ran into that problem before. Hopefully with a little more practice I will remember to use == instead of = inside if statements. Thanks for the help

    One problem down, a new one up. Now that the calculation is not coming up with 1.#INF, I find that the calculation is incorrect. There appears to be no flaw in the math itself, as this is a pretty simple equation. Yet, the numbers are off.

    Example: Input 1, 3, then break the loop with control+z. This returns an average of 3.5 instead of 2.

  4. #4
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    About forgetting '==' and '=' difference:
    You can try using:
    Code:
    if (25 == variable)
    In other words, put rvalue first, and lvalue later. If you forget one '=' there, then you'll get a compiler error, because you just tried to assign to rvalue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM