Thread: i think is a buffer problem

  1. #1
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41

    i think is a buffer problem

    HII everyone !
    i write a programme's code which can choose a word randomly from a txt file
    then the user try to find a characater from the word if the typed character is in the word all the other similar Character will appear
    here is my code just before the probleme is when i type a character the programme displays 2 times the same thing like this :

    you still have 10 turn :
    what is the secret word :
    ******
    propose a character :A




    you still have 9 turn :
    what is the secret word :
    ******
    propose a character :


    you still have 8 turn :
    what is the secret word :
    ******


    propose a character :

    the code :
    Code:
    #include <stdio.h> // include appropriate headers
    #include <stdlib.h>
    #include <string.h>
    
    
    // use define's for constants
    #define MAXWORD 100
    #define FILENAME "test.txt"
    
    
    int main(void) { // use void if you aren't using main's optional input parameters
        char word[MAXWORD];
        char wordtmp[MAXWORD];
        char carac=0;
        char t[MAXWORD];
        int pos = 1,counter=10,i=0,cpt=0;
        FILE* file = NULL;
    
    
        file = fopen(FILENAME, "r"); // use "r" not "r+" if you're just reading
    
    
        if (file == NULL) // deal with file error right here
        {
            fprintf(stderr, "Can't open %s.\n", FILENAME);
            return EXIT_FAILURE;
        }
    
    
        // use fgets return value to control the loop
        while (fgets(word, MAXWORD, file))
        {
            if (pos == 3)
            {
        strcpy(wordtmp,word);
                break; // exit the loop when you find the word
            }
            ++pos;
        }
    
    
        fclose(file);
         while(wordtmp[i]!='\0')
        {
            i++;
        }
    
    
        for(cpt=0;cpt<MAXWORD;cpt++)
        {
            t[cpt]='*';
        }
    
    
       do
    {
    
    
        printf("you still have  %d turn : \n",counter);
        printf("what is the secret word : \n");
    
    
        for(cpt=0;cpt<i;cpt++)
        {
            if(carac==wordtmp[cpt])
            {
                t[cpt]=carac;
                counter++;
    
    
      printf("%c",t[cpt]);
            }
            else
            {
                printf("%c",t[cpt]);
            }
    
    
        }
        printf("\n");
        printf("propose a character   :");
    
    
    scanf("%c",&carac);
        printf("\n \n");
    counter--;
    
    
    
    
    }while(counter>0);
    
    
    
    
    
    
    
    
        return EXIT_SUCCESS;
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your scanf is leaving the newline in the stream, which is being picked up the next time through the loop, causing the problem. Putting a space before %c in the scanf format will skip whitespace before reading a character, which should fix the problem.

    BTW, you should remove all those comments. They were just to tell you the changes I made and do not make a lot of sense as real comments.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    Thank you again oogabooga

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buffer Problem Maybe?
    By Nathan the noob in forum C++ Programming
    Replies: 1
    Last Post: 07-27-2010, 03:39 AM
  2. Buffer problem
    By Narcose in forum Game Programming
    Replies: 3
    Last Post: 03-24-2006, 09:29 AM
  3. buffer searching problem
    By chris285 in forum C++ Programming
    Replies: 2
    Last Post: 04-21-2005, 02:30 PM
  4. buffer overflow problem
    By Renski in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2003, 08:15 AM
  5. Z-buffer problem (?)
    By chomper in forum Game Programming
    Replies: 2
    Last Post: 01-28-2003, 09:23 AM