Не могу понять, почему винчестер не увеличивается, если мы вводим одну и ту же букву «ч» каждый раз - PullRequest
0 голосов
/ 15 декабря 2018

Вот где я взял код из Почему эта программа работает правильно, даже если я ввожу одну и ту же букву "ch". Разве винчестер не увеличивается каждый раз, я думаю, правильная буква.Так что, если я продолжу угадывать одну и ту же букву, она должна увеличить длину винколтера до длины исходного слова, не угадывая другие буквы.например, если в «andrea», если я продолжаю угадывать, он должен объявить меня победителем.Но программа работает совершенно нормально, в чем причина.Почему винчестер остается тем же самым для одного предполагаемого персонажа.

#include <stdio.h>
#include <string.h>
int main()
{
     char word[20];         //Used to store the user word. Most words are 
     less than 20 characters so the default size is 20.
     char stars[20];            //A string full of * that are replaced upon 
     the user entering the right letter of the word.
     int counter = 0;           //A counter to use in the for loops.
     int len;               //len is the length of the word entered.
     char ch;               //ch is a temporary character variable.
     int strikes=0;         //strikes counts how many wrong characters the 
     user has guessed.
     int trigger = 0;           //The trigger variable serves as a flag in 
     the for loops bellow.
     int wincounter=0;          //wincounter keeps track of the number of 
     correct guesses.
     int i;             //Another counter variable to be used in for loops.
     printf("\n\nEnter a word:");           //Ask the user to enter a word.
     scanf("%s", &word);
     len = strlen(word);                    //Make len equal to the length 
     of the word.
     //Fill the stars string with * according to the input word length (len)
      for(counter=0; counter<len; counter++)
      {
          stars[counter]='*';
      }


      stars[len]='\0';                  //Insert the terminating string 
      character at the end of the stars string.


      //Enter main program loop where guessing and checking happens. 26 is 
      //for 20 maximum characters + 6 strike characters.
      for(counter = 0; counter<26; counter++)
      {

          if(wincounter==len)                   //If the number of correct 
          //guesses matches the length of the word it means that the user 
          //won.
          {
              printf("\n\nThe word was: %s\n", word);
              printf("\nYou win!\n");
              break;
          }

          if(strikes==6)                        //If the user makes 6 wrong 
          guesses it means that he lost.
          {
              printf("\n\nThe word was: %s\n", word);
              printf("\n\nYou lose.\n");
              break;
          }

             printf("\n\n\n\n%s", stars);   //Print the stars string 

             printf("\n\nGuess a letter:");  //Have the user guess a letter.

             scanf(" %c",&ch);

             for(i=0; i<len; i++)  //Run through the string checking the 
                                   //characters.
             {
                 if(word[i]==ch)
                 {
                     stars[i]=ch;                   //If the guess is 
                                 // correct, replace it in the stars string.
                     trigger++;             //If a character the user 
                   //entered matches one of the initial word, change the 
                  //trigger to a non zero value.
                     wincounter++;                  //Increase the number of 
                  //correct guesses.
                 }
             }

             if(trigger==0)
             {
                 strikes++;                     //If the trigger is not a 
             //non zero value, increase the strikes counter because that 
             //means 
             //that the user input character didn't match any character of  
              //word.
             }

             trigger = 0;                       //Set the trigger to 0 again 
             //so the user can guess a new character.
      }

      printf("\nPress any key to exit.");
      while (!kbhit()){};                       //Wait for user to hit a 
      //key. This is done to keep results on-screen and not close the 
      //terminal/cmd window right away.
      return 0;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...