Find the answer to your Linux question:
Results 1 to 7 of 7
Hi guys, Having real problem here, I don't understand why this won't loop? What am I doing wrong? #include <stdio.h> int main() { int num = 14; /* initialise number ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Location
    London UK
    Posts
    29

    Why won't this while loop loop?


    Hi guys,

    Having real problem here, I don't understand why this won't loop?

    What am I doing wrong?


    #include <stdio.h>

    int main()

    {

    int num = 14; /* initialise number to be guessed and make number 14 */
    int guess = 0; /* initialise variable for guessing number */
    int i = 0;

    printf("Try to guess the number i am thinking of");

    while (i == 0)
    {
    printf("\nEnter a number between 1 and 100");
    scanf("%d", &guess); /* prompt user to enter a number */

    if (guess > 0 & guess < 101 & guess != num) /* Analyse user input and check results */
    printf("\nGuess you didnt get it try again");

    else
    printf("\nGuess you must have guessed right!");
    i = 1;
    }
    return 0;

    }


    I'm sure this is how I have done this before in C++ and it worked fine. I might be mistaken though.

    Any help would be most welcomed.

    Kind regards

    Jonny

  2. #2
    Just Joined!
    Join Date
    Aug 2011
    Posts
    7
    Alright well I just compiled your code and it looped fine for me. (Until I enter 14 of course) I made no alterations to your code, so maybe something wrong with the compiler?

    Matt

  3. #3
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    Cmon it can't loop just read the code ...
    Code:
    while (i == 0) {
     ...
     i = 1;
    }

  4. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    7
    Close, but that's just part of the error. I forgot to add that I added brackets to the condition statement. Without properly adding brackets to the If/Then the conditions wont execute properly. This code works...

    Code:
    #include <cstdio>
    int main()
    {
    
    	int num = 14; /* initialise number to be guessed and make number 14 */
    	int guess = 0; /* initialise variable for guessing number */
    	int i = 0;
    
    	printf("Try to guess the number i am thinking of");
    
    	while (i == 0) {
    		printf("\nEnter a number between 1 and 100");
    		scanf("%d", &guess); /* prompt user to enter a number */
    
    		if (guess > 0 & guess < 101 & guess != num) { /* Analyse user input and check results */
    			printf("\nGuess you didnt get it try again");
    		}else {
    			printf("\nGuess you must have guessed right!");
    			i = 1;
    		}
    	}
    	return 0;
    
    }

  5. #5
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    Your code still ain't working...
    Try 101 or any bigger digit ... you will always get a you guessed it.

    Well but the while will loop now.

  6. #6
    Just Joined!
    Join Date
    Aug 2011
    Posts
    7
    Well of course the input expected is between 0 and 100 therefore any number less or greater than that range will give an unexpected response. (In this case false to the condition) If you want to do it safely then make the code like this which will force the user to stay in range...

    Code:
    #include <cstdio>
    int main()
    {
    
    	int num = 14; /* initialise number to be guessed and make number 14 */
    	int guess = 0; /* initialise variable for guessing number */
    	int i = 0;
    
    	printf("Try to guess the number i am thinking of");
    
    	while (i == 0) {
    		printf("\nEnter a number between 1 and 100\n");
    		scanf("%d", &guess); /* prompt user to enter a number */
    
    		if (guess >= 0 && guess <= 100 && guess != num) { /* Analyse user input and check results */
    			printf("\nGuess you didnt get it try again");
    		}else if(guess < 0 || guess > 100) {
    			printf("\nDigit out of range! Try again...");
    		}else {
    			printf("\nGuess you must have guessed right!");
    			i = 1;
    		}
    	}
    	return 0;
    
    }
    Last edited by mcross1882; 08-18-2011 at 07:57 PM.

  7. #7
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,598
    Also, you only have single & here instead of two.

    Code:
    if (guess > 0 & guess < 101 & guess != num)
    & = bitwise operator <-- Sets a bit where the corresponding bit in both operands = 1
    && = logical operator <-- This is the one to use if you mean if this is true and this is true

    As an learning exercise, try looking at the difference between 1 & 2 and 1 && 2

    In binary 1 = 0001 and 2 = 0010 so using this for the following examples and remembering that the & will set the result bit to 1 where both are 1 and the && will test the truth (ie where != 0)
    Code:
    0001 &
    0010 
    ====
    0000
    
    0001 &&
    0010
    ====
    0011
    The first evaluates to false and the second to true


    I'm not sure I've explained that well
    Last edited by elija; 08-19-2011 at 11:54 AM.
    "I used to be with it, then they changed what it was.
    Now what was it isn't it, and what is it is weird and scary to me.
    It'll happen to you too."

    Grandpa Simpson



    The Fifth Continent

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •