Thread: hangman game with serious problems!!

  1. #1
    Unregistered
    Guest

    Unhappy hangman game with serious problems!!

    i have got my code working but it just runs a continuous loop of inputting characters...how do i get it to accept the letts either as being in the word and assign them to right place or as a incorrect guess??
    please help no strings!!

    #include <stdio.h>

    #define letters 5

    int search(const char word[], int letternum, char letter);

    int main(void)
    {
    char word[letters] = {'m','o', 'u', 's', 'e'};
    char letter;
    int location;
    int tries= 0;
    {
    printf("choose a letter: ");
    while(tries<6)
    scanf("%c", &letter);
    }
    location= search(word, letters, letter);
    {
    if (location >-1)
    printf("%c was found\n input another letter" , letter);
    else
    printf("input another character:");
    }
    ++tries;
    return 0;
    }

    int search(const char list[], int letternum, char letter)
    {
    int i= 0, found;

    found = 0;

    while ( i<letternum && !found)

    { if (list[i] ==letter)
    found= 1;
    else i++;
    }
    if (found) return i ;
    else return -1;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    It's because all of your {} are out of place

    The code reduces to just these two lines
    while(tries<6)
    scanf("%c", &letter);

    Since you don't modify tries in this loop, this is all this loop will ever do

  3. #3
    Unregistered
    Guest
    hmm so how would i go about fixing this..sorry but im a newbie to this !!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Short answer is start again - big bang programming is rarely effective.

    Build the program in small steps - like before you charge off and see if a char is in an array, get the basic char input routine working.

    Until it works, you're not going anywhere, so there's no point writing more code. It's a lot easier to debug a small bit of code than a large bit.

    Then put that to one side, and try this program
    Code:
    int main ( ) {
       char word[letters] = {'m','o', 'u', 's', 'e'}; 
       int location; 
       location = search ( word, letters, 'a' );
       printf( "Location is %d\n", location );
       location = search ( word, letters, 'e' );
       printf( "Location is %d\n", location );
       location = search ( word, letters, 'm' );
       printf( "Location is %d\n", location );
       return 0;
    }
    Again, more testing of the code in small bits - you can focus on the search, without worrying about input.

    When you've got enough component ideas working, then you can start to bring them together.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. first opengl game, problems.
    By n3v in forum Game Programming
    Replies: 1
    Last Post: 07-11-2006, 08:22 PM
  2. Problems in a game program
    By ChronoSquare in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2006, 07:32 AM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  4. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  5. Memory Problems w/My DX game
    By Stan100 in forum Game Programming
    Replies: 2
    Last Post: 10-03-2003, 01:52 PM