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 ...

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    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;
    }

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    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
    Last edited by The Brain; 10-23-2004 at 06:50 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    no i had that but when i cut and pasted the code on it left it out

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    25
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nim Trainer
    By guesst in forum Game Programming
    Replies: 3
    Last Post: 05-04-2008, 04:11 PM
  2. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-21-2006, 11:23 PM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. Guessing Game Problems
    By Undeadenemy in forum C Programming
    Replies: 7
    Last Post: 05-30-2003, 11:47 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21