Thread: Need help with letter guessing program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    15

    Need help with letter guessing program

    I need help figuring out how to make it so that when you guess, it let's you know you are wrong and tells if the right letter comes before or after the letter you guess. And if you are right it tells you that.

    Also if you find any inconsistencies in my code so far tell me.

    Code:
    #define _CRT_SECURE_NO_DEPRECATE
    #include <stdio.h>
    #define MAXGUESSES 6
    
    
    void Instructions( );
    
    
    int PlayGame(char answer);
    
    
    char  GetGuess(  );
    
    
    int CompareGuess(char guess, char answer);
    
    
    int main()
    {
        //declare additional variables
    char letter=0;
    int gamesToPlay = 0;
    int i = 0;
    FILE *inp;
    Instructions();
    inp=fopen("guessLtrs.txt","r");
    scanf(" %d",&gamesToPlay);
    //get c letter
    
    
        
        //open file
        //get number of games to play
        
        
    
        for(i=0;i<gamesToPlay;i++)
        {
    fscanf(inp," %c",&letter);
    GetGuess(  );
    
    
            //get a letter from file
            //Play one game
            //check for win or lose    
        }
        fclose(inp);
        //close file
        return 0;
    }
    
    //this function provides instructions to the user on how to play the game
    void Instructions( )
    {
    printf("Welcome to Letter Guess\n");
    printf("To begin you will enter the number of games you want to play(1 – 5 games)\n");
    printf("You have 6 chances to guess each letter\n");
    printf("Let's begin:\n\nHow many games would you like to play? (1 - 5):");
    return;
    }
    
    //this function prompts the player to make a guess and returns that guess
    //this function is called from inside the PlayGame( ) function described above
    
    char  GetGuess(  )
    {
        char guess=0;
    printf("Enter a guess:", guess);
    scanf(" %c",&guess);
    return guess;
    }
    
    //this function takes two arguments, the guess from the player and the answer from the file. 
    //It lets the user know if the guess comes alphabetically before or after the answer 
    //The function returns 1 if the guess matches the answer and returns a 0 if they do not match
    
    int CompareGuess(char guess, char answer)
    {
        char value;
        if (guess > answer)value=0;
        else value=1;
    printf("letter come before");
    return 0;
    
    
    }
    //this function runs one game. It for checks either 6 incorrect guesses or correct guess.
    //It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
    
    int PlayGame(char answer)
    {    
        int numGuesses = 0;
        while(numGuesses < MAXGUESSES)
        {    
            //get guess function call
    //compare guess function call
    //update counter for number of guesses used
    numGuesses = numGuesses +1;
        }
        
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What part can't you figure out?

    Take their guess - can you do this step? Why not?
    Compare their guess to the answer - can you do this step? Why not?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    I can take the guess no problem, don't know if i did the comparison right. And if i did it right how do i get text to show you won or lost after to made a guess?
    Last edited by ltdec; 10-04-2011 at 05:52 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not sure why that's confusing to you.
    Code:
    if your letter == my answer
        print out that you guessed, and stop checking
    else
    if your guess is less than my answer
        print out that it is less than the answer
    else
    if your guess is greater than my answer
        print out that it is greater than my answer
    You just need to write out in simple steps what to do.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    I'll try that out, thanks for the help.

    I might have some more questions later on as i progress through this, bear with me.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    I did that but must have done it wrong, here's what i have
    Code:
    int CompareGuess(char guess, char answer)
    {
        if letter==answer,printf("correct");
        else if ( guess < answer),printf("letter comes after");
        else if (guess > answer),printf("letter comes before");
    I get an error of illegal else without matching if.

    I plan on finishing this now, so i value all help you can give.

    I think i put a 1 in place of letter since it says it equals to one if right?
    Last edited by ltdec; 10-04-2011 at 06:20 PM.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by ltdec View Post
    I did that but must have done it wrong, here's what i have
    Code:
    int CompareGuess(char guess, char answer)
    {
        if letter==answer,printf("correct");
        else if ( guess < answer),printf("letter comes after");
        else if (guess > answer),printf("letter comes before");
    If statement requires "(" and ")" around the condition.
    I do NOT think you are using "," operator right; I think it has no place in your solution.

    Please read link on if statements
    If Statements in C - Cprogramming.com Tutorial

    Tim S.
    Last edited by stahta01; 10-04-2011 at 06:24 PM.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     if letter==answer,printf("correct");
    You shouldn't have those commas there. Also, you don't have parenthesis around your comparison.[code]


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    This is my first time taking a class on programming so I will definitely have many problems.

    So i don't use "," with the printf?
    Can someone show me an example of the compare with the printf in it because I'm not sure how to write it.


    edit: I got it to build but when i debugged it it did not add the text after the guesses, i'll play around with it to see what to do. If you know my problem tell me.
    Last edited by ltdec; 10-04-2011 at 06:32 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Before you start fiddling with loading up files and such, you should go with a smaller example to understand how things work. Make a small program that asks you to enter a number, and compares it to a set answer. Enter higher, enter lower, enter a match, get all those different options working. Then you'll understand how to make that part of your bigger program. Don't try to do too much at a time.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    I'm forced to do alot right now, i got a meet a deadline. But, I totally agree.



    edit:I took the function Compareguees and put it into int playgame, since it says it need that function call.

    The problem i got was these errors missing ')' before 'type' and too few arguments for call



    Code:
    int PlayGame(char answer)
    {    
        int numGuesses = 0;
        while(numGuesses < MAXGUESSES)
        {    
            GetGuess(  );
    CompareGuess(char guess, char answer);
            //get guess function call
    //compare guess function call
    //update counter for number of guesses used
    numGuesses = numGuesses +1;
    Last edited by ltdec; 10-04-2011 at 06:45 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ltdec View Post
    I'm forced to do alot right now, i got a meet a deadline. But, I totally agree.
    No you aren't. You don't have to stick it all in the same program at the same time. It takes 30 seconds to stick it into a new program and compile it to test it:
    Code:
    #include<stdio.h>
    int main( void )
    {
        int guess, answer = 5;
        do
        {
            printf( "guess a number:" );
            scanf( " %d", &guess );
            if( guess < answer )
                printf( "too low\n" );
            else
            if( guess > answer )
                printf( "too high\n" );
        } while( guess != answer );
        printf( "good job\n" );
        return 0;
    }
    That was my point. You can write small test programs quickly to test each step, then stick all the steps together.


    Quzah.
    Last edited by quzah; 10-04-2011 at 07:04 PM. Reason: missed a quote
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    I put that in but it did not compile for me :/

    I got these errors
    c:\documents and settings\owner\my documents\visual studio 2005\projects\boot\boot\guess game.c(4) : error C2059: syntax error : 'type'
    c:\documents and settings\owner\my documents\visual studio 2005\projects\boot\boot\guess game.c(7) : error C2001: newline in constant
    c:\documents and settings\owner\my documents\visual studio 2005\projects\boot\boot\guess game.c(8) : error C2146: syntax error : missing ')' before identifier 'scanf'
    c:\documents and settings\owner\my documents\visual studio 2005\projects\boot\boot\guess game.c(9) : error C2065: 'answer' : undeclared identifier
    Last edited by ltdec; 10-04-2011 at 07:04 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah I just typed that up real fast. You could have figured out what the problem was if you listened to your compiler. Anyway, that was sort of my whole point. You can write a small sample to test your program, instead of writing a huge program all at once and then wondering why you have 35 errors and warnings.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Oct 2011
    Posts
    15
    I only got about 2 or 3 errors, if i had over 35 i would be super depressed right now. Not that it might possibly be getting to that point.
    Last edited by ltdec; 10-04-2011 at 07:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with program exercise -- Guessing counter
    By NinjaFish in forum C Programming
    Replies: 8
    Last Post: 04-02-2011, 10:50 AM
  2. Guessing program Hm. Help
    By snugy in forum C Programming
    Replies: 3
    Last Post: 11-17-2008, 11:09 AM
  3. help with guessing game program
    By dc0n in forum C++ Programming
    Replies: 13
    Last Post: 02-07-2006, 08:15 PM
  4. Guessing Program
    By webren in forum C++ Programming
    Replies: 6
    Last Post: 04-10-2005, 07:24 PM
  5. need help with a guessing game program
    By davidnj732 in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2003, 11:59 AM