-2

I'm trying to make program that let the user try to guess the letter that the program choose randomly. The loops and the if statements seem not working. If anybody could check my code a little bit it would be awesome.

I'll attach a pic of the output.

enter image description here

int main()
{
    srand(time(NULL));

    char randlett, playerlett;
    int tries = 0;

    randlett = rand() % 26 + 97;

    printf("The program choose a letter\n\n");

    while (tries < 5){
        printf("Try number %d\nPick a letter\n", tries + 1);
        scanf("%c", &playerlett);

        if (randlett != playerlett){
            if (playerlett > randlett){
                printf("\nThe letter is before the one you chosen\n");
            }
            else{
                printf("\nThe letter is after the one you chosen\n");
            }
            tries ++;
            continue;
        }
        else{
            printf("You win");
            break;
        }
    }

    return 0;
}
Kevin
  • 2,258
  • 1
  • 32
  • 40
NiJuice
  • 13
  • 2
  • Hello and welcome. How is this "not working"? What's the expected behavior and what happens instead? – Federico klez Culloca Jul 30 '19 at 10:28
  • `chars` are integers! They are the shortest integer type available. If you care about whether they can or cannot be negative, use `signed char` or `unsigned char` because plain `char` will be different on different compilers. – pmg Jul 30 '19 at 10:36
  • Yes, you can use chars as integers! And you don't have to write `randlett = rand() % 26 + 97`, you can write `randlett = rand() % 26 + 'a'`, and it will do exactly the same thing. – Steve Summit Jul 30 '19 at 10:39
  • Wirte `int tries=1;` instead of `int tries=0` and then `printf("Try number %d\nPick a letter\n", tries); instead of `printf("Try number %d\nPick a letter\n", tries+1). Or write `tries ++` in the beginng of the `while` loop, not in the end so that you don't have to print `tries+1`. – t3m2 Jul 30 '19 at 11:01

2 Answers2

0
scanf("%c", &playerlett);

As of now scanf is consuming \n(enter pressed) from the previous input.

Change it to,

    scanf(" %c", &playerlett); 
           ^-------------------------//Extra space

or

    scanf("%c", &playerlett);
    getchar();
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
-1

Supplementary to kiran Biradar's answer:

When you use scanf("%c", &playerlett); and you enter a character, you have to press Enter. The problem is that, this Enter is the character '\n', which is used as the next input for scanf in the next interation of the while loop. So, we have to make the program ignore this '\n' (spaces, tabs or enters basically).

This can be done by simply changing the initial scanf to scanf(" %c", &playerlett);. By addind the extra space, you are "telling" the program to ignore spaces, tabs or enters, and only consider other characters as input. So, you can change this line: scanf("%c", &playerlett); to:

scanf(" %c", &playerlett); // this is probably the best solution

Other options (although the first one is the best):

Writing this:

scanf("%c", &playerlett); // this receives the letter eg 'q'
getchar(); // this receives the <Enter>, the '\n'

Or this, very similar:

playerlett=getchar(); // this receives the letter eg 'q'
getchar(); // this receives the <Enter>, the '\n'

An alternative that should not be used: adding adding fflush(stdin) after the scanf("%c", &playerlett); to clean the buffer, basically, to "tell" the program to forget the characters such as '\n' that were entered after the first character: (This is not very recommended, see this answer):

scanf(" %c", &playerlett); // this receives the letter eg 'q'
fflush(stdin); // this "forgets" the <Enter>, the '\n'

See: scanf() leaves the new line char in the buffer for more information.

t3m2
  • 366
  • 1
  • 15