0

I'm very new at coding and I'm learning by myself the C.

I'm doing my very first exercice, I gotta creat a "game"("More or Less") which the concept is :

-Computer choose a random number between and 1 and 100.

-And we gotta guess!

-The game ends when you find the Mystery Number.

I put the function loop (do..while) and also the function (if...else) to keep the game going even if you didn't find the mystery number (unless you find!)

It's been a few days that I'm stuck with my code cause, When I debug, nothing happen (so it's a pretty good news) BUT when I run also NOTHING HAPPEN

My code is:

int main( int argc, char*argv[])

{

    int numberYouChoose = 0;
    int MysteryNumber = 0;
    const int MAX = 100, MIN = 1;


    printf("What's the number?\n");
    scanf("%d", &numberYouChoose);

 srand(time(NULL));
MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;


do{

    printf("Boooooh Try again!");
}while(numberYouChoose != MysteryNumber);

if (numberYouChoose == MysteryNumber);
 printf("Yay you found it!\n");


    return 0;
}
Ringii
  • 3
  • 4

2 Answers2

0

Think like a computer and go step by step... you ask once for a number and you never ask it again so it will be stuck in your do-while forever. You need to ask every time your user fails. Change your Do-while to a simple while

while (numberYouChoose != MysteryNumber) {
    printf("Boooooh Try again!\n");
    printf("What's the number?\n");
    scanf_s("%d", &numberYouChoose);
}

printf("Yay you found it!\n");

Edit:

 if (numberYouChoose == MysteryNumber);
  {
    printf("Yay you found it!\n");
  }

This is redundant, you will exit the whilewhen the user types the right number.

This would be the whole code:

int main(int argc, char*argv[])
{

    int numberYouChoose = 0;
    int MysteryNumber = 0;
    const int MAX = 100, MIN = 1;


    printf("What's the number?\n");
    scanf("%d", &numberYouChoose);

    srand(time(NULL));
    MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;


    while (numberYouChoose != MysteryNumber)
    {
        printf("Boooooh Try again!\n");
        printf("What's the number?\n");
        scanf_s("%d", &numberYouChoose);
    }

    printf("Yay you found it!\n");


    return 0;
}
  • Hi @Juan-carlos-rodriguez, thank you for your answer! I tried to change my code thanks to your advise and I got this now: `code` (...) srand(time(NULL)); MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN; printf("What's the number?\n"); scanf("%d", &numberYouChoose); while (numberYouChoose != MysteryNumber) { printf("Boooooh Try again!\n"); printf("What's the number?\n"); scanf("%d", &numberYouChoose); } if (numberYouChoose == MysteryNumber){ printf("Yay you go it!"); } return 0; } – Ringii Jul 31 '18 at 12:47
  • @Ringii You are welcome, if it helped you dont forget to mark it as the answer and upvote it :) – Juan Carlos Rodriguez Jul 31 '18 at 12:47
  • @Ringii Take a look to my last edit. I gave you a few explanations :D No need to check if the number is right. If you leave the `while` it's because you have guessed the number – Juan Carlos Rodriguez Jul 31 '18 at 12:54
  • Thank you a lot!! Your explanations are really precious to me!! I will try this code right now :) – Ringii Jul 31 '18 at 13:00
0

There are a few problems with your code:

  1. The control structure is wrong as Juan points out in his earlier answer. Simply changing the do-while to a while-do makes your code report 'Boooh...' before the user has even had a chance to make a guess (unless you duplicate asking for a number, which isn't a good idea.
  2. In your line of code if (numberYouChoose == MysteryNumber); the trailing ; is an empty statement, so the printf line following will always be executed.

The following works:

int main( int argc, char*argv[])
{
    int numberYouChoose = 0;
    int MysteryNumber = 0;
    const int MAX = 100, MIN = 1;

    srand(time(NULL));

    MysteryNumber = (rand() % (MAX - MIN + 1)) + MIN;

    while ( 1 )
    {
        printf("What's the number?\n");
        scanf("%d", &numberYouChoose);
        if (numberYouChoose == MysteryNumber)
        {
            printf("Yay you found it!\n");
            break;
        }
        printf("Boooooh Try again!");
    }

     return 0;
}

The ( 1 ) always evaluates to true, so, in theory this will loop forever. However, when you guess correctly, the code reports this and then the break causes the while loop to terminate and the code continues after the while-loop.

Nick
  • 4,820
  • 18
  • 31
  • 47
  • Hello Nick, thank you for your explanation, it's really clear and helped me a lot to understand a lot of things which was absolutely a big question mark when I made this for the first time. I really appreciate, I'm going to try this :) – Ringii Jul 31 '18 at 12:58
  • I found it helpful to put the line `printf( "Clue: %d\n", MysteryNumber );` just before the while loop. It speeded up the testing ;) – Nick Jul 31 '18 at 13:00
  • @Nick You probably have more experience than I do but as far as I know using `break` doesn't follow good practices, am I right? I mean, wouldn't it be better to evaluate in every while iteration ? – Juan Carlos Rodriguez Jul 31 '18 at 13:03
  • @JuanCarlosRodriguez, yes, there disadvantages to using `break`. I chose to here because the lack of a 'repeat-while-do' control structure in C means that we would be forced either to duplicate the user input (as you did) or the success test. See https://stackoverflow.com/questions/3922599/is-it-a-bad-practice-to-use-break-in-a-for-loop – Nick Jul 31 '18 at 13:26