简体   繁体   中英

CS50, Preset 1, Mario More Comfortable - Error: Expected Expression

I'm working on Preset 1 for CS50, Mario More Comfortable. I've been trying to compile the below code, but I'm receiving an 'error: expected expression' message. I've checked the spelling, semicolons, brackets, and parentheses, but everything seems in order. Thoughts?

Code:

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

int main(void)

{
    // declare h

    int h;

    // get integer (user input) between 1 and 8, inclusive
do
{
    h = get_int("Height: \n");

    // if integer is greater than 8 or less than 1, prompt the user again until they enter a valid input
}while(h > 8 || h < 1);

        // outer loop uses i to print a row of bricks h times
        for (int i = -1; i < h; i++)
        {
            // inner loop uses j to print a column h times
            for (int j = 0; j <= h; j++)
            {

                // e.g. if 8 + 8 > (8 - 1), print hash; else print blank: right-aligns
                if (i + j > (h - 1))
                {
                    printf("#");
                }

                printf("  ");

                // e.g. if 8 + 8 < (8 - 1), print hash; else print blank: left-aligns
                else if (i + j < (h - 1))
                {
                    printf("#");
                }

            }

            printf("\n");
        }
    }

Error message:

error: expected expression
                else (i + j < (h - 1))

The expected results of this exercise:

       #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########

You're having error because of the else if

The if statement is offering to computer to check a condition and proceed with a statement inside the curly braces {}. When you exit from these curly braces and write another statement or expression you basicly leaving behind that condition checking. After that if you want to check a condition, you should use if again. You can use else if right after if statement to check if another conditions is valued.

h = 5;
x = 3;
if (h < x)
    printf("h is less than x\n");
x = 10;

if (h < x)
    printf("h is less than x\n");
else if (h > x)
    printf("h is bigger than x\n);
else
    printf("h is equal to x\n");

the program will check the first if condition than evaluate the expression x = 10 then keep reading the next lines and check the second if condition with the new values, then check the else if and later else .

after I've fixed this I've checked your code and it doesn't give the pyramid but square print. If you haven't solved this problem set try to add printf(" "); to print spaces to create pyramid shape. Logic is to decrement the size of spaces while incrementing the size of #'s. If you change your for loop conditions you can achieve the results you want. While you are decrementing the first loop you can increment the nested loop. First loop will print a downward space pyramid while second loop will print a upward # pyramid. Hope make sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM