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

D2Rasta's Avatar, Join Date: Apr 2009
Newbie Member
Hey guys im trying to learn C++.

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

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
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
KibblesCount, is not declared in main and so it would always say that.
listendinesh's Avatar, Join Date: Aug 2007
Go4Expert 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.
xpi0t0s's Avatar, Join Date: Aug 2004
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.