Why am I getting an expected expression error in C?


#1

Hi, I am new to coding and I am working on if-else statements in C. I typed the following code exactly as I learned it but it keeps giving me an expected expression error when I add the else statement. When I remove the else statement it runs fine. I even tried changing the else statement to else if and added an argument but I still get the error. Any help is appreciated! Below is my code:

#include <stdio.h>

int main(void)
{
// Prompt user for pyramid height
{
int i = get_int("Pyramid Height: ");
//Check if integer is between 1-8
if (i < 1 || i > 8)
printf("Please enter an integer between 1 and 8\n ");
}
else
{
printf(“Thank you”);
}

}

And this is the error I get:
make mario clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra-Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow mario.c -lcrypt -lcs50 -lm -omario mario.c:13:5: error: expected expression else ^ 1 error generated. <builtin>: recipe for target 'mario' failed make: *** [mario] Error 1


#2

The structure of an if else statement :


if (condition) {
  do something
} else {
  do something else
}

Mind the brackets.


#3

this looks like CS50…? this is the code for onesided:

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

int main(void)
{
    int n;
do
{
    n = get_int("Height: ");
}while (n < 0 || n > 23);
  for (int i = 0; i < n; i++)
  {
      for (int j = 0; j <= n; j++)
      {
          if ( i + j < (n-1))
          printf(" ");
          else
             printf("#");
      }
      printf("\n");
  }
}

Make sure you try to figure out how to do the double sided version of Mario (More Comfortable). It’s simple once you figure it out, But it takes some time. If you have any more questions feel free to ask or message me. Good Luck!


#4

I was trying many things…I will make sure to check the brackets…thank you!


#5

Yep, it is CS50 lol! I’m planning on trying the double-sided version one next. Thanks for the tips!