0

I want it to loop my printf statement until they give a proper answer from 1 to 23.

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
do
{
    printf("a number from 1-23 please.\n");
    int i = GetInt();
}
while ( i < 0 && i > 24 )

}
1
  • oh, and i get these errors. mario.c:10:9: error: unused variable 'i' [-Werror,-Wunused-variable] int i = GetInt(); ^ mario.c:12:9: error: use of undeclared identifier 'i' while ( i < 0 || i > 24 ) ^ mario.c:12:18: error: use of undeclared identifier 'i' while ( i < 0 || i > 24 ) ^ Nov 2, 2015 at 18:17

1 Answer 1

1

Two problems in your initial code. First, while ( i < 0 && i > 24 ). In your test, when will a number ever be less than 0 AND greater than 24 at the same time? BTW, I believe the limit is 23, not 24.

Next, a more subtle issue. You have declared i inside a pair of curly braces, i.e., the do loop. This defines the scope of the variable i. THat means that i doesn't exist outside of those curly braces. If you delcare i before the do loop, so that it is scoped for the entirety of main, your undeclared variable error will go away.

If this answers your question, please click the check mark and accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .