Reputation: 3
I'm writing a silly code as a joke, sort of a number guessing thing. I thought it was fine until I realized the correct things would only print if I put in the numbers in a specific order. I'm a bit of a beginner, so I'm not sure why it's only printing correctly if I type in the numbers in an order. Is this a condition of while loops in general? Is there a way I can fix this so that it doesn't matter what order the numbers go in? Any insight would be greatly appreciated.
Here is my code:
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
int number;
int main()
{
printf("Enter a number!\n");
scanf("%d", &number);
while ((number != 69) && (number != 420))
{
printf("hmmm, not the number i was looking for... Enter another number!\n");
scanf("%d", &number);
while (number == 666)
{
printf("what are you, emo? try again!\n");
scanf("%d", &number);
while (number == 420)
{
printf("lol close, try the other Funny Number\n");
scanf("%d", &number);
while ((number != 69) && (number != 420))
{
printf("hmmm, not the number i was looking for... Enter another number!\n");
scanf("%d", &number);
}
while (number == 69)
{
printf("haha nice\n");
return 0;
}
}
}
}
}
Upvotes: 0
Views: 178
Reputation: 585
What you may be running into is you enter a number and then it gets stuck in an "inner" loop scanning and checking and failing an inner condition instead of all of them.
I'm not sure if you have yet to discover if/else if/else but this is normally how you might check conditional statements. I will write this in pseudo code to give you a chance to write it yourself in C.
number = 0
print "Enter a number"
while number != 69
number = get number
if number == 666
print "What are you..."
else if number == 420
print "lol close..."
else if number == 69
print "haha nice..."
else
print "hmmm..."
For extra fun check out switch statements.
Upvotes: 1