简体   繁体   中英

Need help looping magic-number program

I have written the below code and need help understanding why it's not working the way it should. It compiles, however, it isn't running the if-else in my loops. For example, if I were to take out the while loop in my code everything would work fine, however, I want to know how many tries it takes for someone to guess the "magic number" or random number in this case.

#include <stdio.h>

int main() 
{

int magicnum = 1234;
int userguess;
int totalguess = 0;


printf("Try to guess a number between 1 and 10000!: ");
scanf("%d", &userguess);


while(totalguess <= 7 && magicnum != userguess);{


  if(magicnum == userguess){
   printf("Congratulations, You Win!!\n");

      if(totalguess = 1){
       printf("Wow you did it on your first try!!\n");
       }

      else(totalguess >= 2); {
      printf("Nice one!! It only took you %d tries!\n", totalguess);
       }
   }

  else if(magicnum > userguess){
   printf("Too Low try again!!\n");
   }

  else{
   printf("Too High try again!!\n");
   }

     totalguess++;
}   
   return 0;
}

I am looking for an output of either someone answering the correct number which is "1234" if they score too high they should see the response of "Too High try again!!", and if they score too low they should see the response of "Too low try again!!. Also, it is supposed to show how many attempts it took them, and if they got it on the first try or not. The max number of attempts a person should be able to do this should be 7.

Problem #1 problem lies in the line

while(totalguess <= 7 && magicnum != userguess);{

Specifically at the semicolon. The above evaluates to the following

// Sit on this line until a condition is met
while(totalguess <= 7 && magicnum != userguess);

// Some block of code which is unrelated to the while loop
{
    ...
}

The answer is to remove the extraneous semicolon at the end of the while loop:

while(totalguess <= 7 && magicnum != userguess) {
//                                No semicolon ^


Problem #2 is in the line

 if (totalguess = 1){ 

Where you are actually assigning totalguess to 1. Fix this by changing = (assignment) to == (comparison).


Problem #3 and #4 are in the line

 else(totalguess >= 2); { 

Not sure how this is even compiling, but you should have an else if rather than an else . And as with the while loop, you have another extraneous semicolon. Remove it.

Lastly, you are only asking for user input once, so the program will loop 7 times without asking for input. Put your scanf inside the main while loop

According to Levi's findings, a solution:

const  int magic_num     = 1234;
const uint max_num_guess = 7;
      uint num_guess     = 1 + max_num_guess;
       int user_guess;

printf( "Try to guess a number between 1 and 10000!\n" );
for( uint idx = 0; idx < max_num_guess; ++idx )
{
    scanf( "%d", &user_guess );
    if( magic_num == user_guess ) { num_guess = 1 + idx; idx = max_num_guess; }
    else
    {
        if( magic_num < user_guess )    { printf( "Too High try again!!\n" ); }
        else                            { printf( "Too Low  try again!!\n" ); }
    }
}
if( num_guess <= max_num_guess )
{
    printf( "Congratulations, You Win!!\n" );
    if( 1 == num_guess )    { printf( "Wow did it on your first try!!\n" ); }
    else                    { printf( "Nice one!! %d tries!\n", num_guess ); }
}

To #3 it is valid. Consider:

if(false){}
else(printf("Branch!\n"));
{ printf("Done.\n"); }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM