0

I'm struggleing with syntax I think. I keep getting errors with expected expressions/identifiers.

I've tried to mimic code I've seen, but I'm not getting it...I'm just trying to set my input and it's getting super frustrating.

The start of my code gives two errors

CODE:

//program to make pyramids of specified heights

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

int main(void)

//user input and value check
{
do

    int h = get_int("Height of Pyramid: ");
}
while (h <= 0 || h < 24)

ERRORS: 
mariotest.c:12:5: error: expected expression
    int h = get_int("Height of Pyramid: ");
    ^
mariotest.c:14:1: error: expected identifier or '('
while (h <= 0 || h < 24)

1 Answer 1

2

You have to declare h before your loop, and not within, otherwise the condition cannot see it. Also, looks like your curly braces are not right. The while surely should be inside your main function, and the do..while should probably get its own set of curly braces.

Additionally, you have a mistake in your condition. Values from 0 to 23, both inclusive, should leave the loop. But for those, h < 24 is true.

8
  • Thanks. I thought the int h after do was introducing the variable, which I guess make sense because you don't want to introduce it every iteration. What do you mean inside the main function, but also needing its own brackets? I think some of that terminology is not defined so well. I assume, leave the loop,means it ends the loop, is that correct? The do while loops are based on exclusive statements? I must have that backwards then. Seriously, this stuff is tough to wrap my head around. All the syntax and terminology is a struggle! Thanks for your help! Jan 29, 2018 at 19:11
  • I am so frustrated. I found other sites thay say the same thing. When I implement it, it doesn't work! What am I doing wrong?!?! From a site: 'do { statement(s) } while (boolean expression);' My code: '{ int h; do { h = get_int("Height of Pyramid: "); } while (h<0 || h>23) }' I have brackets after do, before while and my int is stated before it's used. Still I get the error... 'error: expected identifier or '(' { ^' what is it looking for when it says expected identifier?!? Jan 29, 2018 at 19:39
  • That's super hard to read, but I'm not sure how to get my code to indent in a comment, sorry. Jan 29, 2018 at 19:40
  • If you create the variable within the loop, it exists only for the current iteration, and will cease to exist before the loop condition is reached. You are missing the semicolon after while (h < 0 || h > 23). And there's no way to do multi-line in comments. BTW, you just did not post the int main(void) part, but it's there, right?
    – Blauelf
    Jan 29, 2018 at 22:05
  • yes, it's there, but I am still getting the error at line 8. I have only written 16 lines of code and can't get past this. Please help! Here is my code: #include <stdio.h> #include <cs50.h> int main(void); { int h; do { h = get_int("Height of Pyramid: "); } while (h<0 || h>23); } Jan 30, 2018 at 14:00

You must log in to answer this question.

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