1

I am trying to use the GetInt() function on my variable x and repeat the do while loop if the answer is not between 1-23 but I keep getting undeclared identifier error

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

int main(void)
{
    do
    {
        printf("Pleae enter a number between 1 and 23\n");
        int x = GetInt(); 
        printf("the number is %d\n", x);
    } while (x <= 0 && x > 23);
}

errors

mario.c:11:14: error: use of undeclared identifier 'x'
    } while (x <= 0 && x > 23);
             ^
mario.c:11:24: error: use of undeclared identifier 'x'
    } while (x <= 0 && x > 23);
                       ^
2 errors generated.

then i tried adding int x = 0 and got the following

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

int main(void)
{
    int x = 0;
    do
    {
        printf("Pleae enter a number between 1 and 23\n");
        int x = GetInt(); 
        printf("the number is %d\n", x);
    } while (x <= 0 && x > 23);
}

error

./mario.c: line 4: syntax error near unexpected token `('
./mario.c: line 4: `int main(void)'
0
2

You have initialised two integers of name x. Just declare "int x;" outside your do while loop, and within that loop you simply write "x = GetInt();". Writing "int x" twice and assigning it two different values (zero and the output of the GetInt() function) will have caused your syntax error.

4
  • thank you! i also realized that I needed to use || instead of && for the while comparison. – James Petty Sep 3 '14 at 18:55
  • ok i will try to rethink that. thanks for the heads up – James Petty Sep 3 '14 at 18:56
  • No worries. I deleted my comment because it was a bit confusing, sorry for that. Your code actually seems fine, but you're right, use || instead of &&. – pat_krat Sep 3 '14 at 19:00
  • ok, sweet. thanks again! – James Petty Sep 3 '14 at 19:02
-2

After you compile the program by using make "your program name" (like make mario in this case), then just use ./"your program name" (this case ./mario). DO NOT add .c when running the program.

You must log in to answer this question.

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