0
#include<stdio.h>

void game(int round, int *guess_number, int *player_number, char *status) {
    if(guess_number<player_number&&round<=1)
        printf("Try a smaller number.\n");
    else if(guess_number>player_number&&round<=1)
        printf("Try a bigger number.\n");
    else if(guess_number==player_number)
        status='W';
    else
        status='L';
}

int main() {
    int i,g_number=45,p_number,status;
    for(i=0;i<=2;i++) {
        do {
            printf("Enter a number between 0 to 100:");
            scanf("%d",&p_number);
        } while(p_number<0||p_number>100);
        game(i,&g_number,&p_number,&status);
    }

    return 0;
}

I am waiting for it to guess the number and equalize the status to W if the number is correct, and if it is incorrect, it will give a hint along with the right to try 2. If the last attempt is also wrong, it must be status='L'.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
c_pr
  • 1
  • 7
    The main mistake you're making is ignoring the compiler warnings, or possibly not enabling the compiler warnings. – Paul Hankin Mar 24 '22 at 13:30
  • 1
    Compiler warnings are a pretty good clue: `warning: assignment makes pointer from integer without a cast [-Wint-conversion]`. I think you meant `*status = 'W';` and `*status = 'L';` – Fred Larson Mar 24 '22 at 13:30
  • 1
    You may want to read this: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Mar 24 '22 at 13:31
  • 1
    What Fred says, but also, you're comparing pointers rather than values for `guess_number` and `player_number`. Reading an introductory book on C would be a good first step. – Paul Hankin Mar 24 '22 at 13:31
  • 1
    You don't initialize `player_number`, which is another problem with this code... – Paul Hankin Mar 24 '22 at 13:32
  • 1
    Probably not the reason why your code is not working, but in the function `main`, `status` should be a `char`, not an `int`. Otherwise, you are calling the function `game` with an `int *` instead of a `char *`. Your compiler should be also warning you of this problem (if you enable compiler warnings). – Andreas Wenzel Mar 24 '22 at 13:32
  • @c_pr the link from Andreas should help you, but there's a lot of fundamental mistakes in this code that suggests you need to read more before coding... – Paul Hankin Mar 24 '22 at 13:33
  • 3
    @c_pr, if your compiler is not issuing warnings (not errors) about the code presented then either turn up its warning level or get a better compiler. Possibly it actually is emitting warnings and you do not recognize it -- for instance, because you're using an IDE that puts them somewhere off to the side instead of drawing your attention to them. – John Bollinger Mar 24 '22 at 13:34
  • 3
    *Always* check return value of `scanf()` used on user input. If the user did not enter data in the expected format (numbers, in your case), you may be looking at uninitialized variables. – DevSolar Mar 24 '22 at 13:37

1 Answers1

3

You don't need pointers except for status.

You want this:

#include <stdio.h>

void game(int round, int guess_number, int player_number, char *status) {
    if (guess_number < player_number && round <= 1)   // don't use pointers
        printf("Try a smaller number.\n");
    else if (guess_number > player_number && round <= 1)
        printf("Try a bigger number.\n");
    else if (guess_number == player_number)
        *status='W';   // dereference here
    else
        *status='L';   // dereference here
}

int main() {
    int i, g_number = 45, p_number, status;

    for (i = 0; i <= 2; i++) {
        do {
            printf("Enter a number between 0 to 100:");
            scanf("%d", &p_number);
        } while (p_number < 0 || p_number > 100);
        game(i, g_number, p_number, &status);  // don't use pointers except for status
    }

    return 0;
}

Also note that blank space is your friend.

What is more readable? This:

if (guess_number > player_number && round <= 1)

or this

if(guess_number>player_number&&round<=1)`

Be aware thet the program is not complete, you need to check status after calling game in order to show the user if he won or if he lost.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I tried this but it is not print "Try a smaller number." or "bigger number" – c_pr Mar 24 '22 at 13:38
  • 1
    Look closely at your code, The `round` parameter is pretty pointless. – Jabberwocky Mar 24 '22 at 13:46
  • @c_pr: In my tests, the posted code of this answer does print these two messages for the first two rounds. It doesn't do it for the third round, because that is what you programmed it to do, by adding `&&round<=1` to the `if` conditions. – Andreas Wenzel Mar 24 '22 at 13:50
  • It print just "try a bigger number" – c_pr Mar 24 '22 at 13:55
  • @c_pr: It will also print "Try a smaller number." if you enter a number higher than `45`. – Andreas Wenzel Mar 24 '22 at 13:57
  • if I enter 45,it prints try a bigger number. or if I enter 15 it prints try a bigger number. – c_pr Mar 24 '22 at 14:00
  • 1
    @c_pr: When I run the posted code of this answer, if you enter `45`, then it will print nothing (it will simply set `status` to `'W'` without printing anything). If you enter a number **higher than** `45`, such as `46`, then it will print `"Try a smaller number."`. – Andreas Wenzel Mar 24 '22 at 14:03