0

so ive been stuck on this problem for a about a week, took lots of note and downloaded the lecture just look back on even took notes for the shorts on week one. now what i clearly need is to be taught like a "dumb 5 year old", i wrote this code quickly from memory just to give you an idea. i really want to grasp programming and if i can just the basics on the first problem i can figure out the rest (hopefully). THIS IS OBVIOUSLY WRONG AND WRITTEN IN 2 MINS, I KEEP GETTING THE SAME ERRORS! CANT FIGURE OUT THE SOLUTION..

0

So, let's go over this line by line (skipping some for our own sanity) to get you through what's wrong. This will not make the function pass the checks because your logic isn't quite right, but it will get it to compile and run. With that out of the way, we'll get started at line 3 after your #include statements.

At line 3, int main(void) is the start of the main function of this file. To put it simply, when your computer runs this program, this is what it will look for to start running the program and it will consider the program done when it reaches the end of this section, regardless of what else might be in the file. Because of this, everything you want to happen has be enclosed in curly braces as below, if you want it to run (including calls to other methods that might exist outside of main):

int main(void) {
    commands you want to happen;
    each line must end in a semicolon like these do;
    if a line is exceptionally long, like this one here, and so
             the IDE wraps it to a new line, it still needs a semicolon;
}

Notice how at the end of the function, the closing curly brace is there to tell the compiler/computer that this is the end of the method. Because of the lack of curly braces for your main function, you are getting your present errors as the compiler doesn't know how to interpret your code. Unfortunately, once you fix that you will get more errors, so let's continue and address those.

Moving to line 13 in your image, you have the while statement for your do-while loop. Like the other statements you make in your method, this will need to end with a semicolon to tell the compiler that it is the end. So, add the semicolon at the end of the parenthesis like so: while (height < 1 || height > 8);. This will instruct the computer to rerun the commands between the do and while (inside the curly braces) until the condition of the while has been met.

But, "what about the printf statement in line 16?" I hear you ask. Delete it. The while statement does not itself need commands to execute--here, I think you are getting a do-while loop confused with a while loop and combining the syntax for both. So, everything in lines 15-17 in your image can be deleted.

At line 20, you have a for loop. This for loop needs to have everything that it should execute surrounded by curly braces just as the do-while loop did. So, you need it to look like this:

for(r = 1; r < 8; r++)
{
    the stuff you want to happen;
}

If you wanted your other for loop at line 21 to happen inside of this one, then it needs to be completely enclosed in this one (including its curly braces).

After making all of those adjustments, your program will compile and run. You will only get a vertical line of #s, though, as your for loops need to address the spacing and heights better. As a hint, don't have your for loops go only until a set number, but use the number you got from line 11 so that your program will work for all numbers (right now, it would be a bit weird if I only wanted a pyramid of height 2, for instance).

Hope all of that helps and makes sense. If it did, feel free to accept the answer with the green check mark, but, if you need more clarification, leave a comment and I'll try to help further. :)

You must log in to answer this question.

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