1

I am trying to add a letter to an array which I've defined in a for loop. For each time the user inputs something, I would like to decrease "counterSoFar" by 1, and display it. However, "counterSoFar" always decreases by 2 and skip an opportunity for the user to input anything, instead of decreasing "counterSoFar" by 1. I've tried looking through the code multiple times but I still can't figure out why. Please advise.

# include <stdio.h>
# include <ctype.h>
void userInput(char[]);

int main() {
    printf("Player 1 please enter a word of up to 12 letters.\n");
    char word[13];
    scanf("%13s", word);
    userInput(word);
}
void userInput(char word[])
{
    int n = strlen(word);
    for (int i = 0; i < n; i++)
    {
        word[i] = tolower(word[i]);
    }

    int checker;
    for (int i = 0; i < n; i++)
    {

        if (isalpha(word[i]))
        {
            checker = 1;
        }
        else
        {
            checker = 0;
            printf("Please enter a word without spaces and numbers!\n");
            break;

        }
    }
    if (checker == 1)
    {
        int counterSoFar = 8;
        char letter[1];
        printf("The word to guess is as follows: \n");
        for (int i = 0; i < n; i++)
        {
            printf("_");
        }
        int maxTries = 7;
        for (int guessCounter = 0; guessCounter < maxTries; guessCounter++)
        {
            printf("\n");
            counterSoFar = counterSoFar - 1;
            printf("You have %d tries left!\n\n", counterSoFar);

            printf("Player 2 please enter only 1 letter!\n");
            scanf("%c", &letter);
            char array[13];
            for (int c = 0; c < n; c++)
            {
                if (letter == word[c])
                {
                    array[c] = word[c];
                }
                else
                {
                    array[c] = '_';
                }
            }
            printf("The current array is %c", array);

        }


        }


}
Sarah Collins
  • 225
  • 3
  • 7
  • 18
  • - scanf("%c",&letter); – user2736738 Nov 08 '17 at 13:51
  • The double decrement is caused by the trailing line feed character in stdin. Please read https://stackoverflow.com/questions/35178520/how-to-read-parse-input-in-c-the-faq – Lundin Nov 08 '17 at 13:56
  • It is not responsible for the problem you asked about, but to print array `array`, you must use a `%s` field descriptor (not `%c`, which is for a single character), and you must ensure that the array contains a string terminator. – John Bollinger Nov 08 '17 at 13:59
  • Additionally, I suggest avoiding redundancy. Why create and manage variable `counterSoFar` when the same information is already carried by `maxTries` and `guessCounter` (together). That is, instead of `printf("You have %d tries left!\n\n", counterSoFar);`, I suggest dropping `counterSoFar` altogether and using `printf("You have %d tries left!\n\n", maxTries - guessCounter);`. – John Bollinger Nov 08 '17 at 14:04

3 Answers3

3
  • scanf("%c",&letter);

  • Also you are getting the \n as another input. so use scanf(" %c",&letter); to consume that \n.

Also if you consider few things:-

  1. maxTries is something that you would like to keep outside the for loop. You may ask why? But the thing is it helps in keeping the things the program may need later and it improves readability.

  2. Second thing is if you are using counterSoFar then I guess it is initiated with something. which again backs my first point. That you don't need to reinitialize maxTries in a loop.


As answer to your comment, there are many things you did wrong. I will try one by one.

  • char word[13]; is for 12 character and a null terminating char \0.

scanf("%12s",word);

  • It should be char letter not char letter[1]. Why would you use a 1 size char array but not just one character?

  • char array[13]; should be out of the for loop to retain the already guessed characters.

  • You should initialize it something other than alphabetic characters to distinguish which are being filled which are not.

This is a small example of combining the corrections:- (It would be better if you try incorporateother game logic in this..I have shown a bare minimum portion).


#include <stdio.h>
#include <ctype.h> 
#include  <string.h>
void userInput(char[]);
int main() {
    printf("Player 1 please enter a word of up to 12 letters.\n");
    char word[13];
    scanf("%12s", word);
    userInput(word);
}
void userInput(char word[])
{
    int n = strlen(word);
    for (int i = 0; i < n; i++)
        word[i] = tolower(word[i]);


    char letter;
    printf("The word to guess is as follows: \n");
    for (int i = 0; i < n; i++)
        printf("_ ");

    char array[13]={0};
    for(int i=0;i<=11;i++)
        array[i]='_';
    int maxTries = 16;
    int matched = 0;
    for (int guessCounter = 0; guessCounter < maxTries; guessCounter++)
    {
        printf("\n");
        printf("You have %d tries left!\n\n", maxTries - guessCounter);

        printf("Player 2 please enter only 1 letter!\n");
        scanf(" %c", &letter);

        for (int c = 0; c < n; c++)
        {
            if (letter == word[c] && array[c]=='_')
            {
                matched++;
                array[c] = letter;
                break;
            }

        }
        if( matched == n){
            printf("You won!!!!");
            return;
        }
        printf("The current array is ");
        for(int i=0;i<n;i++)
            printf("%c ",array[i]);

    }
    printf("You lose!!!!");

}
user2736738
  • 30,591
  • 5
  • 42
  • 56
0

You need to change the statement:

scanf("%c", letter);

to:

scanf(" %c", &letter);

as scanf needs to take a pointer as argument, so you should pass the variable's address. The space before %c specifier is needed to consume the \n between the inputs.

Marievi
  • 4,951
  • 1
  • 16
  • 33
0

Change it first correct one is

printf("Player 2 please enter only 1 letter!\n");
        scanf("%c", &letter);
iamsankalp89
  • 4,607
  • 2
  • 15
  • 36