Thread: Help with I/O Files. Hangman Game in C

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Help with I/O Files. Hangman Game in C

    hello. im trying to make a hangman game in C. i made a txt file with the words to be guessed in the game it looks like this:

    Code:
    1 dog
    2 cat
    3 egg
    what i want to happen is i want the user to enter a number. for example i entered "2". so what i want to happen is how would i copy the word "cat" into an array so i could use it as the word to be guessed in my game. sorry for bad english. i would appreciate any tips and help ill be up all night to finish this till monday so please guys any help would be much appreciated

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Since someone may want to play more than one round, I suggest you put a number on the very first line of the file, to tell your program how many words there are that should be loaded.

    Then load all the words beneath the number, into a 2D char array words[][].

    Third section down, file I/O. You want the first part of it, text files:
    C Tutorial - Learn C - Cprogramming.com

    I strongly suggest you bookmark that page - lots faster than asking in a forum.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    thanks adak. now im done with the I/O part. now im stuck. so i was able to read the text file and i am now able to choose my guess word by choosing a number. i only put 10 words to choose from (0-9). i am also able to print how many letters the mystery word has and designate them with asterisks. im kinda stuck from there now. want i want to happen next is to ask the user to enter a letter and update the display with each letter guessed(changing the asterisk with each correct letter guessed),whether its in the word or not. here is what i have so far. like i said i am a beginner and im not that adept at using functions i intend to make this a very simple program just to show the basics of a hangman game can anyone help me finish this?.please

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    void main(void){
    FILE *fp;
    int choice,num,i,j,len,tries=7,found=0;
    char word[10];
    char guess;
    if((fp=fopen("wordlist.txt","r"))==NULL){
    	printf("Cannot open File.\n");
    	}
    
    
    printf("Choose a mystery word by entering a number from 0-9:");
    scanf("%d",&num);
    
    while(!feof(fp)){
    	fscanf(fp,"%d%s",&choice, word);
    	if(num==choice){
    	len=strlen(word);
    
    	printf("Your Mystery Word has %d letters:\t",len);
    	for(i=0;i<len;i++){
    		printf("*");}
    	printf("\n");
    
    	for(j=0;j<tries;j++){
    	printf("You Have %d tries left.\n",tries-j);
    	printf("Enter a letter: \n");
    	scanf("%c",&guess);guess=toupper(guess);
    	???
    	}
    
    	break;
    	}
    	}
    	fclose(fp);
    getch();
    clrscr();
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    feof() doesn't work as you'd expect. I'd replace it as shown below.

    Code:
    include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>
    int main(void){  
    FILE *fp;
    int choice,num,i,j,len,tries=7,found=0,got;
     
    char word[15];
    char guessWord[15];
    char *ptrchar;
    char guess;
    if((fp=fopen("wordlist.txt","r"))==NULL){
        printf("Cannot open File.\n");
        }
     
     
    printf("Choose a mystery word by entering a number from 0-9:");
    scanf("%d",&num);
     
    while((got=fscanf(fp,"%d%s",&choice, word)) > 1) {
        if(num==choice){
            len=strlen(word);
            break;
        }
    }
    fclose(fp);
     
       printf("Your Mystery Word has %d letters:\t %s",len,word);
       for(i=0;i<len;i++){
        printf("*");
            guessWord[i]='*';
        }
        printf("\n");
        guessWord[i]='\0';
     
      for(j=0;j<tries;j++){
           //_gotoxy(10,1);
            printf("You Have %d tries left.\n",tries-j);
            printf("Enter a letter: \n");
            scanf(" %c",&guess);guess=toupper(guess);
            for(i=0;i<len;i++) {
               if(word[i]==guess) {
                  guessWord[i]=guess;
               } 
            }
            printf("%s\n",guessWord);
             
       }
       return 0;
    }
    Try that, and see if it will help.
    Last edited by Adak; 03-09-2013 at 03:35 PM.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    thank you adak. it works now. just a little problem i cant figure out in the first part after i choose a number for my mystery word the asterisks show but also the word to be guessed also prints in the screen. also if i want to make a part where i congratulate the user if he wins or lose is it correct if i use IF function on variable guessword or the variable got? you are a big help adak thank you very much

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would add a space after the colon on line 19. Move line 30 down to 37, and delete the %s and "word" from that line.

    char *ptrchar you don't need - it should be deleted. "got" should be changed to "found", and added to some code, like this:

    Code:
       printf("Your Mystery Word has %d letters:\t",len);
       for(j=0,found=0;j<tries;j++){
          printf("You Have %d tries left.\n",tries-j);
          printf("Enter a letter: \n");
          scanf(" %c",&guess);guess=toupper(guess);
          for(i=0;i<len;i++) {
             if(word[i]==guess) {
                guessWord[i]=guess;
                found++;
             } 
          
          }
          printf("\n%s\n",guessWord);
          if(found==len) {
             printf("\n*** You Win! Congratulations! *** \n");
             break;
          }
             
       }
       if(found<len) {
          printf("\n<<< :( You Lose! :( >>> \n");
       }
       return 0;
    }
    To do more with your game, you'll profit a great deal by using separate functions.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    big thanks to you adak. yeah that was what my prof told me to. i should use functions instead to make the codes look cleaner and easier to read. thanks adak you have saved my life.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Game
    By Mcdom34 in forum C# Programming
    Replies: 3
    Last Post: 10-13-2012, 11:21 AM
  2. Hangman game
    By Dontgiveup in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2011, 04:44 PM
  3. Help with hangman game.
    By astarialexi in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 04:04 AM
  4. Hangman Game - Need Help!!
    By krobort in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2006, 04:15 PM
  5. New game: Hangman!
    By abrege in forum Game Programming
    Replies: 6
    Last Post: 12-05-2002, 02:05 PM