1

I'm trying to write a mastermind program but I'm stuck with string inputs. I try get user guess and when I try to print it it gives error I don't know why here is that part of the program

char* UserGuess[4];

void *Guess()
{
    long i;
    printf("Enter your guess: ");
    for(i=0;i<4;i++)
    {
        fgets(UserGuess, 4, stdin);

    }
    return UserGuess;       
}   
int main()
{
    int userchoice=0, i;    

    while(userchoice!=2)
    {

        Guess();
        printf("%s\n", UserGuess[0]);
        break;
    }
}
2
  • userchoice doesn't change at all in your loop. And the call to Guess() is useless since you ignore the return value. UserGuess is an array of pointers but you seem to be using it like an array. There are too many basic issues in your code. Better read a proper text book. Commented Aug 5, 2016 at 11:53
  • userchoice is not matter at this moment I am just trying the program and why guess function is useless it is recording the data to the array and array is defined as global variable Commented Aug 5, 2016 at 11:56

3 Answers 3

1

Lets take a look at these two lines:

char* UserGuess[4];

fgets(UserGuess, 4, stdin);

The first declares and defines UserGuess as an array of four pointers to char. I.e. as four strings.

The second tries to use it as an array of char, i.e. a single string.

This is wrong and you probably want something like

for (unsigned i = 0; i < 4; ++i)
    fgets(UserGuess[i], SOME_SIZE, stdin);

That of course leads to another problem, because all the pointers in UserGuess will be null pointers (global variables are zero-initialized, which means pointers become NULL). So you have to change the array definition as well:

char UserGuess[4][SOME_SIZE];

Or you only want a single string of three character (plus the terminator)? Then you should change the definition of the array to

char UserGuess[4];

and change the output to e.g.

printf("%s\n", UserGuess);

Then on a somewhat related note, the fgets functions reads the newline and might add it to the string (if it fits).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your comment, I changed it as you said but I want user to enter four colors like; red green black white the press enter and it will store it in string and when I print it like userchoice[0] will be equal to red. Is it possible to do this with your given code above ?
@HasanBilalAdak Then look at the first section of my answer.
0

In your code, UserGuess is an array of pointer-to-char, but all the elements, are invalid (NULL). You need to allocate memory to them before you can write into team.

That said, with fgets(), you need to pass the individual elements of the array.

Something like

char UserGuess[4][32];

and

for(i=0;i<4;i++)
{
    fgets(UserGuess[i], 32, stdin);

}   

can be a good start.

Comments

0

You can probably use scanf("%d",&n) where n is an integer instead of going this route.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.