Math program (small) compiling and math errors .

Started by
4 comments, last by Zahlman 14 years, 11 months ago
Hey guys im trying to learn C++. This was a simple program i made while reading C++ For dummies

#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
 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
Advertisement
The compiler issue is that you didn't declare KibblesCount when you passed it in your main() function.

It should have looked like
int main(int argc, char *argv[]){    int KibblesCount = 0;    BigDog(KibblesCount);    system("PAUSE");    return 0;}


That would fix the compiler error, but you don't need to pass a variable to your BigDog function at all. You could simply do it like this:

int BigDog() {    int NumCount;    cout << "How many kibbles should we feed the doggie?" << endl;    cin >> NumCount;    cout << "I'm a lucky dog" << endl;    cout << "I have " << KibblesCount<< " doggie treats" << endl;    cout << "RUFF RUFF RUFF" << endl;    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();    system("PAUSE");    return 0;}


You don't need KibblesCount at all.
Gotcha!

Thanks for your help!

Edit: Is there a command for C++ that just make a blank line besides just doing

Cout << " ";
Yup. In your previous 'cout' phrase, stick a new line in there. \n.

Example.

std::cout << "Ruh Roh!\n\n" << std::endl;

that will output 'Ruh Roh!' plus 2 new lines.
Quote:Original post by D2Rasta
Edit: Is there a command for C++ that just make a blank line besides just doing

Cout << " ";

cout << " " doesn't output a blank line. If you want a blank line, you either have to use:

cout << "\n"
or
cout << endl
Though you should know that '\n' != endl. endl inserts a new line into the system and then flushes the system. '\n' is just a new line character.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
The reason for giving a function a parameter is so that you can pass information into the function. Here, BigDog() reads the information that is needed, so there is no reason to use a parameter.

If you turn up all the warnings on your compiler, it should warn you that 'NumCount' is uninitialized. It is, indeed, uninitialized. Uninitialized variables are often a sign that you didn't do things quite like you thought you did. Here, you declare a variable called 'NumCount' (what's that supposed to mean, anyway? Of course any kind of count is a number...), but read 'KibblesCount'. Then you assign from NumCount into KibblesCount. Since NumCount is uninitialized, it could hold basically anything at this point, so you are playing around with a garbage value. This is not what you want to do. I assume you meant to assign the other way around, but there is no point to using another variable.

There is only one count variable that you need: KibblesCount. It is initialized within BigDog(), so we don't pass it, because there is nothing meaningful to pass. (The reason you were told it was undeclared is because it is undeclared within main(). By putting a declaration "at the top", you are adding yet another variable - a global one - and arranging for main() to pass this uninitialized, global variable into BigDog().) Since BigDog() needs the variable, and we won't be passing it in, we declare it within BigDog(). We initialize it by reading it in, and then use it.

Putting that all together, we get:

// By the way, don't just go around randomly #include'ing headers. You should// learn what each is for. In your case, you are only using things from <iostream>.#include <iostream>using namespace std;int BigDog() {  int KibblesCount;  cout << "How many kibbles should we feed the doggie?" << endl;  cin >> KibblesCount; // Now it has a known value.  cout << "I'm a lucky dog" << endl;  cout << "I have " << KibblesCount << " doggie treats" << endl;  cout << "RUFF RUFF RUFF" << endl;  // Now we use KibblesCount directly.  while (KibblesCount > 0) {    cout << "Num num num num thanks!" << endl;    KibblesCount--;  }  cout << "Ruh Roh! I Are All Out!" << endl;}int main(int argc, char *argv[]) {  // Our function takes no parameters, so calling it looks like this:  BigDog();  // I have taken away the pause because you should not artificially pause  // your programs at the end. Instead, run them from the command line, or  // using a batch file that implements the pause.  // in C++, main() is a special function: you do not need to 'return 0;' at  // the end. If main() reaches the end, it returns 0 automatically. This is  // NOT the case with any other function; if you write a function that returns  // a value, it must return something at the end (if the function hasn't  // already returned in the middle). BigDog() is fine, however, because its  // return type is 'void', meaning that it doesn't return a value.}


By the way, C++ is case sensitive. "Int" does not mean the same thing as "int", and "Cout" does not mean the same thing as "cout". You should get in the habit of being careful about these things, even when you're just talking about the code instead of writing it; that way you're less likely to mess things up when it matters. :)

This topic is closed to new replies.

Advertisement