This program is in C. Basically I am trying to guess a secret number (15), But t
ID: 3562625 • Letter: T
Question
This program is in C. Basically I am trying to guess a secret number (15), But the program doesnt recognize when i get it right. No compiling errors were there either so i am confused!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 15, y;
printf("Guess a number! ");
scanf("%i", &y);
while (y != x)
{
for (y=1; y<=14; y++)
{
printf("guess higher ");
scanf(" %i", &y );
}
for (y=16; y>=100; y++)
{
printf("guess lower ");
scanf(" %i", &y);
}
}
if (y == x)
{
printf("congrats ");
}
return 0;
}
Output (guess 15) (not coming out the way i want it too)
12 guess higher 18 guess lower 15 guess higher 22 guess lower 9 guess lower
Explanation / Answer
you need a while loop instead of for loops you have used.
also only writing
while (y<=14)
{
printf("guess higher ");
scanf(" %i", &y );
}
while (y>=16)
{
printf("guess lower ");
scanf(" %i", &y);
}
instead of
for (y=1; y<=14; y++)
{
printf("guess higher ");
scanf(" %i", &y );
}
for (y=16; y>=100; y++)
{
printf("guess lower ");
scanf(" %i", &y);
}
would be correct. :)
Happy to help!!!! CHEERS !!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.