I have been struggling with this for weeks now, have done several searches and posts and can not seem to get around this. I have tried everything and it just makes it worse. I wish there was a video on compiler errors and how to fix them. As far as I can tell my code matches all of the examples I have seen, what am I missing?
2 Answers
Happy to help. You're running into syntax errors, which you'll have a lot of as you're getting started. Always best to cure them in order because sometimes the early ones cause a lot of later ones.
I think the reason you're not finding the first one is that it's one of those deceptive ones where the problem isn't on the line where it's being signaled. Once you see it though, you'll never do it again! ;-)
The problem is on line 6, not line 10. You don't have the correct form for main. Instead of a semicolon, there should be an opening curly brace. A main (or any) routine should have the following form:
int main(void)
{
// code goes between curly braces for main.
// don't put a ; after main(void)
}
That should cure your first two bugs. Next is the printf error. You have nicely defined what should be printed out, including formatting, but you forgot to tell it where to get the value to print. Based on your code, it should look something like this:
printf("your pyramid will be %i high\n", i);
A few comments on this. First, note the addition of , i
near the end of the statement. This tells the formatting placeholder %i
to use the value in the variable i there. In %i, the "i" is a formatting character (put an integer here), not to be confused with your variable i, that has been added. Also, I changed %d to %i because the variable i is an integer, not a double. (Although, I believe it would still work by casting i as a double for the printf statement.) For every placeholder in the quoted string, you need to have a corresponding variable after the string.
I see you put curly braces around the printf() statement. These weren't needed here. (You may have added the opening one because of a reported mismatch error when you really needed one just after the main statement.) Curly braces define blocks of code and scope for variables. Any var defined within those curly braces will cease to exist outside of those braces. Just be aware/careful of that.
Finally, may I suggest that you go back and review the material on style. Also, in the beginning, be sure to run your program through style50 to get a feel for better coding style - indentations, use of white space, etc. You'll really benefit from a better understanding of indentation. Better to learn now before you develop any bad habits. ;-)
This should get you going. If you have any more questions, search the forum and if you don't find anything, feel free to post more questions.
If this answers your question, please mark this question as answered. Let's keep up on forum maintenance. ;-)
================================ Edit: added after response.
You need to get a good handle on lines and blocks of code. Ordinarily, every line of code ends with a ; semicolon. There are exceptions.
For one, functions, including main, can end with either a ;
or a code block in curly braces { .... }
. btw, a ;
is a do-nothing after a closing }
and is not required. A function followed by a ;
is (almost?) always just a prototype of a function to come later. Later, the prototype would be repeated exactly the same, except that it will be followed by the curly braces with the function code between them. For example,
int getnumber(void);
may appear before the lines
int main(void)
{
//main code block
}
to tell the compiler that the code for getnumber()
is coming later, even though the function is called in main()
.
The next thing to wrap your head around is the concept of code blocks. A code block is enclosed by a pair of curly braces.
{
// a block of code goes here.
}
Any variables defined inside of these braces only exist within them. Important concept here is that a do/while loop requires them around the lines of code to be executed. (There may be a way to do a single line without them, I don't remember, but not the best practice.)
So, your first error on line 11 is simply a missing semicolon. The second error is that you didn't have the curly braces between do and while.
do
{
printf(...
I = .....
}
while (condition);
After that, some errors are probably due to earlier errors. You'll find that some errors can mask others, while other errors cause things to be marked as errors later, even though they are not. Try these on for size. If you still have problems, maybe it's time for a new question, or go over to one of the chat-type resources. Slack and the subreddit (referenced from the class main page) will usually have someone around that can help real time.
Good luck!
Thanks for your answer, It helped shed some light on some of the things that I missed . Unfortunately it has gotten me no closer to solving all of my problems. In searching for an answer to my problems I saw examples of the main(void) with and without the semi colon. I would get fewer errors with the main(void); statement than with the main(void) statement. This appears to be my first mistake. I equated fewer errors as "better" I think that I add it semi colon after that line error indicated in green ( I think that means a correction??) that the semicolon was to be added to that line. I still get errors on the do while part of the loop. I have gone back the shorts and the lectures again and again and do not see the differences for the examples and what I have written. To add to my frustration the video on loops in the shorts section hangs at 2:22 so that I cannot see what he is talking about.
-
The usual procedure is to add comments to a question or an answer, or to edit the original question to add more. Save the "answer" mechanism for actual answers, even to your own questions. Having said that, check the edited version of my answer. ;-)– Cliff BCommented May 8, 2015 at 23:34