0

I get the error "Expected Expression" on line 45:9 at the else statement in the for else loop.

Any ideas? I've been working on this pset for around 7 hours but can't figure it out.

    // Includes the cs50 and standard IO libraries
#include <cs50.h>
#include <stdio.h>

// Declares the variables "n", "i", "l, and "w" as integers
int n; // height
int i; // height difference (invalid)
int l; // length
int w; // width


// Declares the main function "main()"
int main(void) {

    // Checks whether n is a number between 0 and 23 and asks for a new input if false
    do
    {
        printf("Please input a positive number under 23: ");
        n = GetInt();

        // If the userinput is above 23, the height difference will be displayed
        if(n > 23){
        i = 23 - n;
        printf("Height: %i", i);
        }

        // If the userinput is below 0, the height difference will be displayed
        else if (n < 0) {
        i = 0 + n;
        printf("Height: %i\n", i);
        }
    }
    // Keep running the do loop until either "n" is below zero, or "n" is above 23
    while (n < 0 || n > 23);  

    // If the number is between 1 and 23, then print pyramids with a height according to "n"

    for(l = 1; l <= n; l++)
    {
        for(w = 1; w <= n; w++)
        {
            printf("#");
        } 

        else if {
            printf(" "); 
        }

        printf("\n");

    }

    while(n == 0)
    {
        printf(" ");
    }

 return 0;  

}
1

That line has two serious problems.

        else if {

First, an else clause must immediately follow an if statement and it's associated block of code (enclosed in curly braces). Otherwise, it is syntactically wrong.

Second, after the else, you have an if with no test condition. Again, this is syntactically wrong and won't compile. If you wanted an if test on this line, you should simply have if (test condition) { <some lines of code here } or maybe a do loop, or just the code you want to run.

You also have a problem later in your code.

while(n == 0)
{
    printf(" ");
}

If n does equal 0, this produces an infinite loop. Nothing inside the loop will change n to something else, so it will just keep looping forever. Perhaps you wanted an if statement here? But to print a single space? No idea what you're trying to accomplish.

There are other aspects of the code that don't make sense for this pset, such as the processing done in the first do loop.

I think you would benefit greatly if you review the class material covering for loops, while and do/while loops, and if statements and else clauses. Also, one of the subtle lessons of this class is to write code to do exactly and precisely what is asked for in the assignments. Since check50 looks for exact output, any variations - extra or missing characters, spaces, carriage returns, prompts, etc. - will almost always trigger a fail in the result. The same check50 that you can run locally is also used to grade all of the assignments.

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

You must log in to answer this question.

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