Results 1 to 7 of 7
Thread: Why won't this while loop loop?
|
Enjoy an ad free experience by logging in. Not a member yet? Register.
|
|
-
08-17-2011 #1
- Join Date
- Aug 2011
- Location
- London UK
- Posts
- 29
Why won't this while loop loop?
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
-
08-18-2011 #2
- 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
-
08-18-2011 #3
- Join Date
- Dec 2009
- Posts
- 269
Cmon it can't loop just read the code ...
Code:while (i == 0) { ... i = 1; }
-
08-18-2011 #4
- 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; }
-
08-18-2011 #5
- Join Date
- Dec 2009
- Posts
- 269
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.
-
08-18-2011 #6
- 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.
-
08-19-2011 #7
Also, you only have single & here instead of two.
Code:if (guess > 0 & guess < 101 & guess != num)
&& = 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
I'm not sure I've explained that wellLast edited by elija; 08-19-2011 at 11:54 AM.
Should you be sitting wondering,
Which Batman is the best,
There's only one true answer my friend,
It's Adam Bloody West!
The Fifth Continent