bad math?

This is a discussion on bad math? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <string> using std::cout; using std::endl; using std::cin; using std::string; int main() { int number, number1, number2; ...

  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
    21,717
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    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, 08: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, 07: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

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