1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Math program (small) compiling and math errors >.<

Discussion in 'C++' started by D2Rasta, Apr 30, 2009.

  1. D2Rasta

    D2Rasta New Member

    Joined:
    Apr 30, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys im trying to learn C++.

    This was a simple program i made while reading C++ For dummies :p

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    
    int BigDog(int KibblesCount) {
         int NumCount;
        cout << "How many kibbles should we feed the doggie?" << endl;
        cin >> KibblesCount;
         cout << "I'm a lucky dog" << endl;
         cout << "I have " << KibblesCount<< " doggie treats" << endl;
              cout << "RUFF RUFF RUFF" << endl;
              KibblesCount = NumCount;
              while(NumCount > 0) {
                    cout << "Num num num num thanks!" << endl;
                    //NumCount--;
                    }
                   cout << "Ruh Roh! I Are All Out!" << endl;
    }
         
    int main(int argc, char *argv[])
    {
        BigDog(KibblesCount);
        system("PAUSE");
                return 0;
    }
    
    First it said, KibblesCount is undeclared. So i put
    Code:
     Int KibblesCount 
    at the top.

    Then, when i put an integer to basically "Feed the dog" it spammed it then exited.


    What this prog does
    --------------------
    You experts know but for the lazy, im trying to make it so when you enter an integer, it will say Num Num Num that many times.

    Thanks in advance
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
    KibblesCount, is not declared in main and so it would always say that.
     
  3. listendinesh

    listendinesh New Member

    Joined:
    Aug 3, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    KibblesCount = NumCount;
    NumCount is a uninitialized local variable and it will contain the garbage value.
    Any operation on NumCount will give unpredictable results.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    > You experts know

    Actually we don't. It's impossible to tell by looking at a program that doesn't do what it's supposed to do, precisely what it is that it's supposed to do, without some kind of description. The code that shows what the program IS supposed to do isn't there, obviously.

    Looks like "KibblesCount = NumCount;" is the wrong way round. You want to copy KibblesCount to NumCount, not the other way round (although why you want to copy it instead of just decreasing KibblesCount in the loop isn't clear).

    As "NumCount--;" is commented out, "(NumCount > 0)" will always evaluate TRUE and you have yourself there a good old infinite loop. Very happy doggy indeed, until he explodes.

    "BigDog(KibblesCount);" why do you need to pass anything in to BigDog()? It doesn't seem dependent on any parameters.
     

Share This Page