12
\$\begingroup\$

This is my first program in C. Do you have any improvements to this program? The program allows you to guess a number and also returns the attempts.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
/*
 * Sophie
 * 21.03.2021
 */
void main() {
     int secret;
     int guess;
     int try = 1;

    time_t t;
    srand((unsigned ) time(&t));
    secret = rand() % 100+1;

    printf("Please Enter value:\n");
    scanf("%i", &guess);

    while (guess != secret){
        if (guess > secret)
            printf("Guess too high. \n");
        else
            printf("Guess too low. \n");

        printf("Please enter a new value:\n");
        scanf("%i", &guess);
        try++;
    }
    printf("You gussed the number! \n");
    printf("You needed %i Tries.", try);

    getch();
}
\$\endgroup\$
1
  • \$\begingroup\$ This is a great program to learn with!  The first program I ever wrote — in BASIC on a borrowed Video Genie, no less — was a guessing game.  Though after all this time I can't remember whether the computer picked the number and the human had to guess it (as here), or vice versa…  Both are good, so once you're happy with one, try the other! \$\endgroup\$ – gidds Mar 22 at 14:25
4
\$\begingroup\$

I also liked the program - it did not compile, but that was easy to fix - what are <conio> and getch()?

I enjoyed a game or two, until I wondered if it was 0-99 or 1-100. The spacing of % 100+1 was a bit misleading.

I simplified the time() call.

Then I wondered why there are two scanf() calls. The input messages are different (please enter / please enter NEW), but the input itself is the same. So I ended up with while(scanf() > 0).

It is not so clear what to put in that while condition. Also what to do with invalid input. My version stops and prints the number of tries.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* modified number guessing game, with while(scanf()) loop */
/* original by Sophie 21.03.2021 */

void main() {
    int secret, guess;
    int try = 0;
    srand(time(NULL));
    secret = rand() % 100 + 1;

    printf("Please enter first guess (1-100):\n");

    while (scanf("%i", &guess) > 0) {
        try++;
        if (guess == secret) {
            printf("You guessed the number!\n");
            break;
        }
        if (guess > secret)
            printf("Guess too high. ");
        else
            printf("Guess too low. ");

        printf("Please enter a new value:\n");
    }

    printf("You tried %i times.\n", try);
}

I am afraid you have to sacrifice that nice

while (guess != secret) {

and concentrate more on the scanf() return.

(Well with that negation != it wasn't even that great - more like "trapped in eternal loop as long as guess is wrong")

\$\endgroup\$
6
  • 2
    \$\begingroup\$ The spacing of % 100+1 was a bit misleading don't fix that with spaces, use braces: secret = (rand() % 100) + 1; \$\endgroup\$ – Paul Evans Mar 22 at 10:47
  • \$\begingroup\$ @PaulEvans I'd call those brackets, round brackets, parentheses, or parens; braces are curly brackets — { and } — and have a very different meaning in most programming languages!  But yes, whatever you call them, they're useful for clarifying precedence.  This case is a bit subjective; I'd consider % similar enough to division that the precedence was already obvious — but if it's not obvious to you, by all means use brackets. \$\endgroup\$ – gidds Mar 22 at 14:30
  • \$\begingroup\$ Your version still doesn't compile (error: invalid prototype for main function). \$\endgroup\$ – Konrad Rudolph Mar 22 at 14:43
  • \$\begingroup\$ @gidds I should have looked it up and used parentheses, round brackets, or first brackets but I think the idea is clearly conveyed by the example. And don't consider % similar enough to division that the precedence was already obvious use parentheses to make your code totally clear! Be kind to your readers and to your future self, make reading your code as easy as possible. \$\endgroup\$ – Paul Evans Mar 22 at 16:04
  • \$\begingroup\$ @gidds "I'd consider % similar enough to division that the precedence was already obvious." Obvious what the precedence is or obvious what precedence the previous coder intended? \$\endgroup\$ – Kevin Mar 22 at 20:46
15
\$\begingroup\$
  • void main is a really bad habit. It must be int main.

  • Always check what scanf returns. For example, try to enter a non-numeric input, and see your program entering the infinite loop.

  • Avoid conio.h (and hence getch). It is very non-portable.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ It could be worth mentioning why is void main a bad habit. I don't think it's good to tell someone that something is bad, without telling why. (I believe the reason is because it is not valid C code, altho some compilers will ignore it and still compile it, but i might be wrong.) \$\endgroup\$ – AnswerMyQuestion Mar 22 at 17:42
  • \$\begingroup\$ Yes, the C standard mandates that main() returns an int. Moreover, the program needs to return a status code to the operating system, and this return value is how you do it. (Sometimes embedded systems don't have an operating system to return to, but usually they will either specify a different function to use or else they will still use int main() even though the return value is meaningless.) \$\endgroup\$ – Kef Schecter Mar 22 at 21:39
3
\$\begingroup\$

It looks good for a first program in C, good job.

An improvement would be to ask the user to insert a number which will be the maximum number to guess (instead of the default 100 value).

You also could change the try variable name to tries.

\$\endgroup\$
2
  • \$\begingroup\$ I think there are arguments both for try (which makes more sense within the loop) and tries (which reads better below it).  (I might pick tries myself, but probably from my experience in languages like Java and Kotlin where try is a keyword, which obviously doesn't apply here!) \$\endgroup\$ – gidds Mar 22 at 14:33
  • \$\begingroup\$ That's right. "attempts" could also be a great name for this variable. \$\endgroup\$ – Kfir Ram Mar 22 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.