1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

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

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

  1. D2Rasta

    D2Rasta New Member

    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

    KibblesCount, is not declared in main and so it would always say that.
     
  3. listendinesh

    listendinesh New Member

    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

    > 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