Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Math program (small) compiling and math errors >.< (http://www.go4expert.com/forums/math-program-small-compiling-math-errors-t17230/)

D2Rasta 30Apr2009 07:04

Math program (small) compiling and math errors >.<
 
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

shabbir 30Apr2009 08:33

Re: Math program (small) compiling and math errors >.<
 
KibblesCount, is not declared in main and so it would always say that.

listendinesh 30Apr2009 10:07

Re: Math program (small) compiling and math errors >.<
 
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 30Apr2009 21:50

Re: Math program (small) compiling and math errors >.<
 
> 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.


All times are GMT +5.5. The time now is 04:50.