Home > Mobile >  Can you Help me correct this simple c program's little mistake?
Can you Help me correct this simple c program's little mistake?

Time:11-24


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
    
int main()
{
    int num, rnum, times = 1;
    srand(4383);
    rnum=rand() % 300   1;
    while(times <=8)
    {
        printf("Guess the numper random number between 1-300: ");
        scanf("%d", &num);
        if (num<rnum)
        {
            printf("The random number is biger\n");
        }
        if (num>rnum)
        {
            printf("The magic number is smaller\n");
        }
        if (num == rnum)
        {
            printf("RIGHT!");
            break;
        }
        times  ;
    }
    printf("FAILURE!");
    return 0;
}

The point of the task is to make a program for a user to type and try to guess a numper from 1–300 with 8 attempts. If you find the number it shows RIGHT! and if not it guides you by telling that the number is biger/smaller. If you fail in your 8 atemts then it shows failure. The problem is that it shows failure when you fail to guess in your 8 atempts but when you find the number it prints both RIGHT & FAILURE. What should i correct for the program to print failure only when you cant’t find the number within your 8 tries?

CodePudding user response:

My 2 cents, path of least resistance is to simply return rather than break when the user guesses correctly:

if (num == rnum)
{
    printf("RIGHT!");
    return 0;  // program exits here, no FAILURE print
}

You should also seed the rand function with a changing number, like time. With a constant, you'll find your number to guess is the same every time.

srand(time(NULL)); // randomize seed

CodePudding user response:

You should check if they exceeding the 8 try limit before executing the print statement:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
    
int main()
{
    int num, rnum, times = 1;
    srand(4383);
    rnum=rand() % 300   1;
    while(times <=8)
    {
        printf("Guess the numper random number between 1-300: ");
        scanf("%d", &num);
        if (num<rnum)
        {
            printf("The random number is biger\n");
        }
        if (num>rnum)
        {
            printf("The magic number is smaller\n");
        }
        if (num == rnum)
        {
            printf("RIGHT!");
            break;
        }
        times  ;
    }
    if (times > 8) 
    {
        printf("FAILURE!");
    }
    return 0;
}

CodePudding user response:

I think you should write return 0; instead of break; after printing RIGHT. because if you guess the rnum it will print RIGHT and breaks out and after that it will print FAILURE too but if you write return 0; it will end the program.

CodePudding user response:

When you break; out after having printed RIGHT! you end up where you print FAILURE! so you need to check this somehow.

Here's my take on it:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int num = 0, rnum;
    // This game will get boring with a static seed.
    // I'm assuming you use this seed for testing only.
    srand(4383);

    rnum = rand() % 300   1;

    for(int times = 0; times < 8;   times) {               // simpler loop
        printf("Guess the random number between 1-300: ");

        // if the user fails to enter a number - make it print FAILURE
        if(scanf("%d", &num) != 1) break;

        if(num < rnum)
            puts("The random number is bigger");
        else if(num > rnum)                         // added "else"
            puts("The magic number is smaller");
        else
            break; // neither less nor greater than rnum so it must be correct 
    }

    if(num == rnum)          // added check
        printf("RIGHT!\n");
    else
        printf("FAILURE!\n");
}
  •  Tags:  
  • c
  • Related