Thread: bad math?

  1. #1
    human jerkey dead_captain's Avatar
    Join Date
    Jan 2008
    Location
    509
    Posts
    37

    Question bad math?

    Code:
    #include <iostream>
    #include <string>
    using std::cout;
    using std::endl;
    using std::cin;
    using std::string;
    
    int main()
    {  
       int number, number1, number2;
       const int AVERAGE = number + number1 + number2 / 3;
        
        cout << "Pick a number!: ";
        cin >> number;
        cout << "Another number: ";
        cin >> number1; 
        cout << "Last number!!: ";
        cin >> number2;  
        
        (number + number1 + number2);
        cout << "\nThe avereage is: " << (AVERAGE) << "\n";
        
         
        system("\nPAUSE");
        return 0;
    }
    I get 9333568 eveytime. y?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    At the time when you initialised AVERAGE, the values of number, number1 and number2 were garbage since they were not initialised. Garbage in, garbage out, so the saying goes, thus AVERAGE contained garbage when you printed it.

    What you should do instead is:
    Code:
    int number, number1, number2;
    
    cout << "Pick a number!: ";
    cin >> number;
    cout << "Another number: ";
    cin >> number1; 
    cout << "Last number!!: ";
    cin >> number2;  
    
    int average = (number + number1 + number2) / 3;
    cout << "\nThe average is: " << average << "\n";
    Note of course that this is integer division, so the result may not be what you expect.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    human jerkey dead_captain's Avatar
    Join Date
    Jan 2008
    Location
    509
    Posts
    37
    cool. thanks.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you had maximum warning levels turned on, the "variable used without being initialized" warning should have been your first clue.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And if you used Visual Studio (2002+), then you'd get a runtime error on the average line
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  2. Math Question
    By Joselo16 in forum C++ Programming
    Replies: 8
    Last Post: 05-20-2005, 08:46 PM
  3. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 09:26 AM
  4. Math, Physics and their relation to Programming
    By biterman in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-19-2001, 08:45 AM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM