Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is designed to make the user guess random numbers. Cannot get this

ID: 3638831 • Letter: T

Question

This program is designed to make the user guess random numbers.

Cannot get this program to run gives me an error at the break statement, nor am I sure the random number genorator is working right.

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

int main(void)

{
int i;
int guess;

printf("I have a random number between 1 and 1000 ");
printf("Can you guess my number? ");
printf("Your Guess:");
scanf("%d", &guess);

srand(time (NULL) );

for ( i = 0; i < 1001; i++);

(i +1, 1 + ( rand() % 6) );

if ( guess > i) {
printf("Your guess was too high. ");
}

if ( guess > i ) {
printf("Your guess was to lw. ");
}
if( guess = i){
break;
}



return 0;
}

Explanation / Answer

Break statements are only used in loops and switches, so it its giving you an error because it's in an if statement. How you have the program structured right now is with if statements instead of loops, meaning it will let you guess once, check the if statements, then quit. Also, a good way to see if something is working or not is to print it out, for example after your rand statement you can try printing i to see if it randomizes every time. Here's an example of what you're trying to do; it generates a random number between 1 = 1000 and prints out what its going to be (Just to show you) The if statements are inbetween a do while loop, so it will repeat until the correct answer is typed. If you only want it to go through once, you can just delete the do while loop.


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

int main ()
{
int i, guess;

/* initialize random seed: */
srand ( time(NULL) );

/* generate secret number: */
i = rand() % 1000 + 1;
printf ("DEBUG: I is %d ", i);

do {
    printf ("Guess the number (1 to 1000): ");
    scanf ("%d",&guess);

    if (i < guess)
        printf("Your guess is too high");
    else if (i > guess)
        printf("your guess is to low");
       
} while (i!=guess);

printf("Right!");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote