2
votes

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.

2
Can you please provide your current output and your expected.. - Chand Priyankara
I have edited the post with the expected output of what I should see. All the output works if I don't use the while loop, however I want people to be able to try again if they don't get the random number on the first try, which is why I included the while loop so they have a chance to get it right. - Derrick Rose
Voting to close as simple typo(s). - Lundin
@Lundin If you haven't noticed this post has actually been answered, and was my very first post on SO. Whether this gets closed or not doesn't affect me. - Derrick Rose
@AsadMahmood The purpose of closing posts where the problem was a simple typo is that the question, as well as all answers, are not of interest to anyone but the poster themselves. You have done nothing wrong in posting this here, but future readers simply have no interest in reading about your syntax errors. So the close isn't meant to affect you, you have already received the answer you needed. - Lundin

2 Answers

4
votes

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 ^


if (totalguess = 1){

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


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

0
votes

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"); }