Thread: watz wrong here?

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    25

    Question watz wrong here?

    Hey,
    This is the part of my code for Hangman game where player make a correct guess and the program should display it as (for ex) _ _ _ s _ _ s _

    len is the lenght of the word.
    word is the word that computer randoms
    guess is the letter the player choose

    Code:
    for(k=0;k<len;k++){
        if(guess==word[k]){
          index=k;
          for(i=0;i<len;i++){
    	if(i!=index)
    	  printf("-\n");
    	  else
    	    printf("&#37;s",word[index]);
          }
        }
     }

    what's the problem here?!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Uhh yeah, like we can see the problem with such a short bit of code. The code is entirely dependant upon the variables, so post more code to illustrate the problem!
    You could also indent a little better. Use a tab (or 4 spaces) on each block - increases readability. If and else should be on the same indent level - again, increases readability.
    And I also hope that you declared word to be a 2D array.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    sorry :P here it comes the whole program.
    (actually not the whole. but I'm still working on this part.)


    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<time.h>
    
    int main(){
    
       int x,i,j,k,h,len,dr,index;
       char guess;
       char flt[9][20];
       char* word;
      
      FILE *sep; 
      sep=fopen("hangman.dat","r");
      for(i=0;i<9;i++){
        fgets(flt[i],20,sep);
      }
      fclose(sep);
      srand( time(0) );
      j=rand()&#37;8+1;
      word=flt[j];
      len=strlen(word);
      printf("%s",word);
      while(true){
        printf("Guess a letter:\n");
        getchar();
        scanf("%c",&guess);
        for(k=0;k<len;k++){
          if(guess==word[k]){
    	index=k;
    	for(i=0;i<len;i++){
    	  if(i!=index)
    	    printf("-");
    	  else
    	    printf("%s",word[index]);
             }
           }
         }
       } 
        return 0;
    }
    Last edited by sifeet; 12-16-2007 at 04:17 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so does that compile? Because true isn't a keyword in C.
    Missing ; at the printf and the if.
    h, x and dr are unreferenced.
    return 0 at the end of main is never reached due to your endless loop.
    And also check if you failed to open the file and print an error!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Quote Originally Posted by Elysia View Post
    OK, so does that compile? Because true isn't a keyword in C.
    Missing ; at the printf and the if.
    h, x and dr are unreferenced.
    return 0 at the end of main is never reached due to your endless loop.
    And also check if you failed to open the file and print an error!
    yes, it does compile! I just maybe missed the ";" sign when copy-pasting. and those unreferenced variables are for the other part of the code which I put comment sign around. and strangely it is not occurred as an error!
    anyway it compiles and I can run it.
    for ex the random word is organization. I type "g" and what is displayed afterward is 12 times of this character " _" ! it never checks the letter I entered =(

    and why is the loop endless? I'm trying to traverse the string and that is not endless!?!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    getchar fetches your first input and scanf later fails.
    And the actual while(true) loop is endless.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    gcc compiler or Visual C

    which compiler are you usinig?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I use Visual Studio 2008.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i can see why u are using getchar before scanf function, but i would suggest to use just the getchar function and remove scanf call. Get the return value getchar in a variable and then process on it.

    And about the problem itself. Why dont use read on complete word from the user rather than eah char. So u can read one complete word and check on each char, thats makes it more simple rather than complicating on these loops.

    ssharish

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    25
    Quote Originally Posted by t3chn0n3rd View Post
    which compiler are you usinig?
    I guess you were asking me, who started this thread. I use MinGW. and I write in Xemacs.
    well. I still have the same problem. I removed scanf . and use only getchar.
    I can not get the whole word from the player. Cause this is how the game is. computer randoms a word and the player tries to guess the word by guessing letters used in the word. so I have to have those loops!

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    printf("%s",word[index]);
    This is broken - you are printing a single character as a string - you probably want %c instead of %s.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM