Thread: need help with a guessing game program

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Question need help with a guessing game program


    //The following program I wrote is a guessing game
    //program. The program chooses a random number, which the user should
    //attempt to guess. Hints are given to the player instructing him/her
    //that the guess is too high or too low. The program ends if either
    //the correct number is guessed, or the user does not guess the number
    //within 10 tries. If the user guesses the correct number, the
    //program proceeds to reports
    //a. the number of tries it took to guess the number.
    //b. The incorrect guesses made by the user.

    //Using structures I declared a data-type called struct PlayingPerson.
    //This structure should keep track of a player's name and score.
    //The score is defined to be the number of guesses the user needed to guess the correct answer.
    //The struct PlayingPerson should be given an alternate name Player,
    // using the typedef statement.
    //A file called HighScores.dat should contain the name and
    //score of the player with the lowest score.
    //(Note: the lower the score, the better the player).
    //The first thing your game should do is open this file and
    //read in the name and score contained within it. Also, the
    //current player's name should be read in from the keyboard.
    //After the game is ended, the score of the current player
    //is compared with that read in from the file.
    // If the score of the current game is less than that read
    //in from the HighScored.dat file, the current name and score
    //are recorded in the HighScores.dat file, thereby replacing
    //the old data in it. If this is the first time the program is run,
    //the file HighScores.dat does not exists yet.
    //If then your program attempts to open this file,
    //it will fail. In this case the file should be created at
    //the end of your program, and the current score and name recorded
    //in it.


    #include <iostream.h>
    #include <iomanip.h>
    #include <stdlib.h>
    #include <time.h>


    struct playingperson
    {
    char playersname[50];
    int score;



    };

    typedef struct playingperson player;

    void populate (player &populateplayingperson);
    void print (player printplayingperson);



    int main ()
    {
    player a;
    populate(a);
    print(a);
    int guesses[10];
    int y,x;
    int zero =0;

    srand((unsigned)time(NULL));
    int mynumber=rand();

    mynumber=(mynumber%100)+1;




    cout<<"I've got a number from 1 to 100, can you guess it?"<<endl;


    for(y=0;y<10;y++)

    {

    cout<<"Your Guess: ";
    cin>>guesses[y];
    zero++;

    if (guesses[y] > mynumber)
    {
    if (zero == 10)
    {
    break;
    }
    cout<<"Guess Lower."<<endl<<endl<<"Guess again"<< endl;

    }
    else if (guesses[y] < mynumber)
    {
    if (zero == 10)
    {
    break;
    }
    cout<<"Guess Higher."<<endl<<endl<<"Guess again" << endl;
    }

    else
    {
    cout << "Congratulation YOU GOT IT!!" << endl;
    break;
    }

    }
    for(x=0;x<zero;x++)
    cout<<"Guess "<<x+1<<" was: "<<guesses[x]<<endl;

    cout<<"The correct answer is: "<<mynumber<<endl;
    cout << "You guessed " << zero << " times." << endl;
    return 0;
    }
    void populate(player &populateplayingperson)
    {
    cout<<"Players Name: "<<endl;
    cin>>playingperson.playersname;

    cout<<"Players number of guesses: "<<endl;
    cin>>playingperson.score;
    }
    void print (player printplayingperson)
    {
    cout<<"Players Name: "<<player.playersname<<endl;
    cout<<"Players number of guesses: "<<player.score<<endl;
    }



    I can not seem to get this to work correctly. Could someone steer me in the right direction. Any help would be appreciated.

    Thanks

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I think 'zero' is possibly one of the worst identifiers ever. Seeing code like
    Code:
    if(zero == 10)
    makes my head spin. You wouldn't need that short story at the top of your program if you wrote self-documenting code. One way to do this is to use descriptive identifiers so that someone else reading your code will instantly recognize what a variable is and what you're trying to do.

    edit: And please use code tags.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    27
    I did this once for a class I took. You might try putting it all in a while loop, such as: while (guesses[y] != mynumber)
    "Computers aren't intelligent, they only think they are."

    **infected by Blizzarddog**
    I am a signature virus. Please add me to your signature so that I may multiply

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    as salem said, use the correct parameters in both of your function....whatever u have in the function declaration will be the name of the variable for that function

    Code:
    void print (player printplayingperson)
    {
    cout<<"Players Name: "<<printplayingperson.playersname<<endl;
    cout<<"Players number of guesses: "<<printplayingperson.score<<endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  2. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 03:05 AM
  3. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  4. My number guessing game
    By The Gweech in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2002, 09:03 AM
  5. Help with guessing game im making
    By Unregistered in forum C++ Programming
    Replies: 14
    Last Post: 06-06-2002, 09:14 PM