# Guessing Number Game Problem

• 10-23-2004
cgod
Guessing Number Game Problem
i tried writing a guess the number game using functions but i ran into some problems
Code:

`8 C:\Documents and Settings\wmorrish\Desktop\New Folder\main.cpp ` guesscount' undeclared (first use this function)`
Code:

` 10 C:\Documents and Settings\wmorrish\Desktop\New Folder\main.cpp ` guess' undeclared (first use this function)`
would i have to redeclare guess and guesscount inside the function? or is there any other way around this?
here is my code so far
Code:

```#include <ctime> #include <cstdio> using namespace std;     inline void guessagain() {     cout<<"Guesses Left"<<guesscount<<endl;     cout<<"Guess:"<<endl;     cin >> guess;     guesscount = guesscount -1; }  int main() {     srand((unsigned)time(0))     int number = rand()%10;     int guess;     int guesscount;         cout<<"The Computer Has Thought Of A Number Try And Guess It"<<endl;     cout<<"you have 10 guesses."<<endl;     cout<<"Guess : ";     cin >> guess;     guesscount = guesscount - 1;     if(guess == number)     {         cout<<"Well Done!! You Guessed The Random Number "<<number<<" With "<<guesscount<<" Tries Left"<<endl;     }     else if(guess > number)     {         cout<<"To High, Try A Lower Number."<<endl;         guessagain();     }     else if(guess < number)     {         cout<<"To Low, Try A Higher Number"<<endl;         guessagain();     }      return 0; }```
• 10-23-2004
The Brain
1.) you forgot #include<iostream>

2.) int guesscount is never initialized with a maximum number of guesses.

3.) guesscount = guesscount -1 could be --guesscount

4.) Since 'guess' is not a global vairable (it was created inside of int main() i think you will return a new 'guess' back to main from your guessagain() function:

Code:

```inline int guessagain()  //function will return an 'int' {   int guess;  //declare a local guess variable       cout<<"Guesses Left"<<guesscount<<endl;     cout<<"Guess:"<<endl;     cin >> guess;  //variable can now be used since it has been declared     guesscount = guesscount -1;  //problem here as well, 'guesscount' is also not global.                                 //i would probably leave this operation in int main()   return guess;    //return the user's guess back to main }```
and although global variables are sometimes frowned upon, you could always make guesscount global.. where it can be accessed by any function:

declare guesscount before int main( ), giving it global scope:

Code:

```int guesscount = 10; int main( ) {```
Then whenever a function wants to access this global variable, just precede guesscount with the scope resolution operator.. which will give guesscount global scope outside of the function.

Code:

```inline int guessagain( )  //function will return an 'int' {     int guess;        cout<<"Guesses Left"<<guesscount<<endl;     cout<<"Guess:"<<endl;     cin >> guess;     ::guesscount = ::guesscount -1;    return guess; }```
Just keep in mind, the reason people don't like to use global variables... is that they can be easily accessed and changed by any function. I personally think globals are ok.. they are similar to private class member variables with the exception of being limited to only class member functions. I think at this stage in the game, use of globals are o.k. until you learn classes. Remember that all local function variables are destroyed after function execution.. thus the need for some sort of class or global access.

then, when you are back inside int main( ), you can make this type assignment (if you wanted to) for example:

Code:

`int guess = guessagain( );      //Function call returns number of remaining guesses`
• 10-23-2004
cgod
no i had that but when i cut and pasted the code on it left it out
• 10-23-2004
boozy
hmm.. why not use a loop? should be easyer and better to use I would think.

[code]

#include <iostream.h>
#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
int tryes = 10;
do {
tryes--;
cout<<"Guesses Left"<<tryes<<endl;
cout<<"Guess:"<<endl;
cin >> guess;
;
;
; //rest of program
;
;
}
while (tryes >= 1);
cout << "\nYou are I am sillyI am sillyI am sillyI am sillyed;
system("pause");
}

[\code]

or at least I would have made it more like this.:P but Im more a newbie then you:P