Thread: Nonfunctional function

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    Nonfunctional function

    Hi all, I wrote this function for a hangman game I am making. This function is supposed to take in prompt for a letter as a guess then check it against two arrays, one wrongly guessed letters and one with right guesses. What happens is that it asks for a guess then you enter a letter and it just stops. You can keep hard returning, but it doesnt do anything past that.

    Thanks for the help in advance,

    Evan

    Code:
    int getGuess(char display[], char wrong[], int misses) 
    { 
    char guess; 
    int valid; 
    printf("Enter a character to guess:"); 
    guess=scanf("%c",&guess); 
    valid=1;
    
    while(valid==1)
    {
    if(guess>='a' && guess<='z')
       {
       while (*wrong != '\0' && *wrong != guess )
        {    wrong++;
    
        if( *wrong == '\0' )
            valid=1;
        else
            valid=0;
       }
    while (*display != '\0' && *display != guess )
        {    display++;
    
        if( *display == '\0' )
            valid=1;
        else
            valid=0;
       }
      }
    if(valid==0)
       {
        printf("Try again!");
       }
    }
    return guess;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > while (*wrong != '\0' && *wrong != guess )
    If you keep wrong and display as proper strings, then simply use
    if ( strchr( wrong, guess ) != NULL ) // guess is in wrong
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM