Guessing Number Game Problem

This is a discussion on Guessing Number Game Problem within the C++ Programming forums, part of the General Programming Boards category; i tried writing a guess the number game using functions but i ran into some problems
Code:
8 C:\Documents and ...

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.

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: