-1

I'm somewhat new to C/C++ and I don't get what my problem in the following code is. In this assignment I am restricted to use C-functions and syntax only, no C++ allowed.

The programm should choose a random character from the alphabet and give the user 3 tries to guess it.

#include <stdio.h>
#include <stdlib.h>
#define guesses 3

int main(){

    setbuf(stdout, NULL);
    /* A 2.P.b */

    /* version with loop*/

    int i = 0;
    char randomChar = (65 + (rand()%26) + 1);

    /* comment the line below, when the programm works as intended */
     printf("\n%c\n", randomChar);

    char guessedChar;

    while((i<=guesses) && (guessedChar != randomChar)){

        printf("Guess a letter.\n");
        scanf("%c", &guessedChar);

        if(guessedChar != randomChar){
            int guessesLeft = guesses - i;
            if (guessesLeft > 1){
                printf("Wrong letter. You have %d more tries.\n", guessesLeft);
            }
            else{
                printf("Wrong letter. Last try.");
            }

        }

        else{
            printf("Congratulations.\n");
        }

        i++;
    }
    return 1;
}

The output is as follows:

Q
Guess a letter.
A
Wrong letter. You have 3 more tries.
Guess a letter.
Wrong letter. You have 2 more tries.
Guess a letter.
B
Wrong letter. Last try.Guess a letter.
Wrong letter. Last try. 

The problems:

The chosen letter is NOT random, but always Q.

The first guess takes costs two "lives" ?!

Alex
  • 751
  • 1
  • 6
  • 34
  • 1
    Two questions in one. Both duplicates. http://stackoverflow.com/questions/8724582/rand-not-generating-random-numbers-after-modulo-operation http://stackoverflow.com/questions/3744776/simple-c-scanf-does-not-work – undur_gongor Oct 29 '14 at 12:58
  • 3
    Did you **read the documentation** of [rand(3)](http://man7.org/linux/man-pages/man3/rand.3.html) & of [scanf(3)](http://man7.org/linux/man-pages/man3/scanf.3.html), as you should? If you did, you won't have asked the question. Also, try to understand what RTFM & STFW mean. and compile with all warnings & debug info `gcc -Wall -Wextra -g`) BTW, C & C++ are *different languages* – Basile Starynkevitch Oct 29 '14 at 12:59
  • and maybe GIYF and LMGTFY for slightly less offensive alternatives along the same lines.. – bph Oct 29 '14 at 13:03
  • 1
    A small tip about style, it's recommended that you macros defined by the preprocessor (like `guesses`) you should use capital letters only, as in `GUESSES`. It's not mandated by the language or the preprocessor, it's just what is commonly used just about everywhere and by everyone. – Some programmer dude Oct 29 '14 at 13:07

6 Answers6

1

Probably, you always get Q because you don't seed the generator. Try adding

srand ( time(NULL) );

before generating it.

Andrea
  • 6,032
  • 2
  • 28
  • 55
1

The problem with scanf is that the newline after the letter you enter is still left in the input buffer, and will be read the next time around.

To solve this simply tell scanf to read and skip all leading whitespace by adding a single space in the format string, like

scanf(" %c", &guessedChar);
//     ^
//     |
//     Note space here

The problem with the random number generation is that you don't seed the generator, which means it will always start with the same seed and always generate the same number.

To set the seed you need to call the srand function before calling rand, which a non-predicable seed (the current time is usually enough):

srand(time(NULL));

There is however a much more serious problem, and that is that you use the guessedChar before it's initialized. This will lead to undefined behavior because uninitialized non-static local variables have an indeterminate value. You must initialize local (non-static) variables before you use them, and the only valid use of an uninitialized variable is to initialize it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You need to use srand at the start of main for random alphabets(Don't forget to include time.h!)

srand(time(NULL));

And as for the problem with scanf,add a space before %c in the scanf:

scanf(" %c", &guessedChar);

This is done to remove the \n which will be present in the stdin after you enter a character as you pressed the enter key after typing the character.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

Of course. rand() generates a pseudorandom number. It is enough random for many purposes like this. However, you have to change the starting point, otherwise you'll get always the same numbers. You should call srand(something); and for the something, you may substitute any number, which will allow you to have plenty of remembered know "games" or you can initialize that by current time (including miliseconds), which will be very unprobable repeatable.

#include <time.h>       /* time */

int main(){
srand (time(NULL));
...
undur_gongor
  • 15,657
  • 5
  • 63
  • 75
V-X
  • 2,979
  • 18
  • 28
-1

Actually rand() is nt really random because it gets number from an fixed array, so each time you are calling rand() you are getting the same number so with the same equation you gonna get the same character , then for the first guess , you are testing befor getting answer that s why you should use

do
{
}
while ();

instead of

while()
{
}
Taryn
  • 242,637
  • 56
  • 362
  • 405
indian
  • 130
  • 8
-3

if you are just taking a character in input , try to use

getch();

instead of

scanf(...);

and

do

{
}
while ();

instead of

while()
{
}

since you take an answer then you do the test ...

indian
  • 130
  • 8