0

I’m trying to write a program for the “bulls and cows” game. The game explanation: The program has a secret 4 digit number. The user has to guess the numbers. If the number is: 1235 And the use guessed: 1592 The number of bulls is 1, because the user guessed a correct number and the position of it, And the number of cows is 3, Because the user only guessed the numbers.

I’m trying to make the guess function, but the output is weird. It’s still not a function, just trying to make it work first. I have to use an array of chars and an int. the user needs to have an option to type 0 as one of the digits.

The code:


    #include <stdio.h>
    #include <conio.h>
    /A function that gets 4 chars from the user and checks if they're valid
    
    int main()
    {
    char guess[5]= {0};
    int i=0;
    
    printf("please enter a 4 digit interger\n");
    for (i=0;i<4; i++) {
    scanf("%d", &guess[i]);
    printf("your guess is:");
    for (i=0;i<4; i++) {
    printf("%d", guess[i]);
    } 
    }
        
    getch();
    return 0;
    
    }

The weird output is:

Your guess is: -1281300

Why doesn’t it show the number the user gets? Thank you so much for helping!

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
  • 1
    If you want an integer as an input, then read it into an `int` variable. If you want it as string - then read it with `%s` format specifier. If you want to read character by character, then the format specifier is `%c` – Eugene Sh. Jun 07 '21 at 18:27
  • 1
    `%d` is the wrong specifier for `char`. You want `%c`, or perhaps `%s` to read the whole string at once. – 0x5453 Jun 07 '21 at 18:27
  • 1
    You are ignoring the return value of scanf at your own peril. In this case reading, outputting and checking it would tell you a lot about what is going wrong. – Yunnosch Jun 07 '21 at 18:39
  • So there's a sequence of four random IID from `[0,9]`, and you guess by the oracle that we are given the number of correct sequence matches (bull) and the number of correct set matches (cow)? – Neil Jun 07 '21 at 18:43
  • Please look [here](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings) and [here](https://godbolt.org/z/jssnx3aza) and [here](https://stackoverflow.com/questions/59812014/why-use-conio-h). – n. m. could be an AI Jun 07 '21 at 18:45
  • I'm new here so I don't know how to answer to a specific comment, but thank you so much everyone! You're saving me. <3 – Paparazzixfever Jun 07 '21 at 18:47
  • 1
    Welcome! If you are new you can obtain your first 'badge' by reading [the tour](https://stackoverflow.com/tour) and may find this topic and others in the help centre useful too: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Weather Vane Jun 07 '21 at 18:50

1 Answers1

1
  1. The %d format specifier is for ints, which are generally larger than chars. You're probably writing to memory outside guess[i]. %c is the format specifier for char.
  2. Your write loop is inside your read loop. You read to guess[0], then immediately print guess[0] through guess[3], but all but guess[0] are still 0. (unless they got clobbered due to (1)).
Ray
  • 1,706
  • 22
  • 30