As an addendum - when it comes to error messages and warnings from gcc (and other compilers/interpreters) the error messages usually tell you the exact line where an error occurs.
However, sometimes the
ACTUAL problem isn't on the line mentioned in the error message. But rather, on the line immediately BEFORE.
So for example, in your original post - where the compiler was complaining:
Code:
Draw_Poker.c: in function getBet():
Draw_Poker.c:269:5 error expeced `while` before `int`:
269 | int analyzeHand(int ranksinHand[], int suitsinHand[])
| ^
I imagine when you first saw that error, the first thing you thought was:
"But that's the start of the definition of my analyzeHand function! Why does it say it expects `while` instead of `int`?! And why does it say that the error is in the getBet function?!"
But in this case, the compiler isn't complaining that line 269 is wrong. It's actually saying that it was expecting a
while
statement at line 269 - so something went wrong somewhere
before that line.
So, as an inexperienced programmer - how do you work out how to solve the problem?
Well, in your original code, what you would have needed to do was look up a few lines at your getBet function:
C:
// Function to get the users' bet between 1 and 5
int getBet()
{
int bet;
do //Will keep running until the user enters 0-5
{
printf("How much do you want to bet? (Enter a number ");
printf("1 to 5, or 0 to quit the game): ");
scanf(" %d", &bet);
if (bet >= 1 && bet <= 5)
{
return(bet);
}
else if (bet == 0)
{
exit(1);
}
else
{
printf("\n\nPlease enter a bet from 1-5 or ");
printf("0 to quit the game.\n");
}
} << Last line of code BEFORE the error
// Last function revviews the final hand and determines the value of
// the hand.
int analyzeHand(int ranksinHand[], int suitsinHand[]) << compiler says this line is wrong!!
{
int num_consec = 0;
int i, rank, suit;
// More code below here
Above is your original code.
So looking at the line before the line being complained about (ignoring any lines with comments) - the last actual line of code was a closing brace - and to you - it
looks like the closing brace for your
getBet
function. How could that possibly be wrong?
Well, the next thing to do is take a look at the entire
getBet
function.
So start at the beginning of the function, go through everything and ensure that all of the braces match up.
e.g. any opening braces have corresponding closing braces.
And that everything else is syntactically correct.
Also make note of the
do
loop in your function. It's worth remembering that
do
loops require a corresponding
while
condition
after the closing brace for the
do
loop.
So the next step is to trace your way through the braces to find the closing brace for the do loop. And you'll see that the final closing brace for the
getBet
function is actually the closing brace for the
do
loop
AND that you're also missing a brace to close the function.
So a
while
condition needs to be added after the closing brace for the
do
loop AND you need a final closing brace to end the function definition.
And that would fix that particular problem! And then you'd move on to the next error message.
In reality, I imagine that when you typed out the original code, you probably just accidentally missed out the line that contained the closing brace for the
do
loop and it's corresponding
while
condition and only entered the closing brace for the function.
In doing that, your braces were mis-matched AND as we've seen the block of code for the
do
loop was not being terminated correctly.
So when you compiled the code - the compiler reached the closing-brace that
YOU thought marked the end of the function definition. When in fact, from the compilers point of view - that brace actually terminated the
do
loop. And immediately after the
do
loop, the compiler expects to see a
while
condition.
Also, because the final closing brace for the function was missing, the very next thing the compiler found was
int
- which was the return type for your
analyzeHand
function. Which is why it complained about the
int
in the line for the
analyzeHand
function definition
AND said it was inside the
getBet
function.
Which is why it threw that potentially confusing error.
Now, if you imagine that the closing brace for the
getBet
function
was there.
AND that the
while
condition was missing from the
do
loop -then you would have got exactly the same error message as before. It would complain that the error was in the
getBet
function, but the line of code it complained about would be the closing brace for the function.
So again - the problem would be on the line immediately
before the error - because we're missing the
while
condition on the previous line, which is the closing brace for the
do
loop!
I hope that makes sense?!
Interpreting compiler errors and warnings in any programming language can be tricky sometimes, it's not always immediately obvious where the actual error is. But it's a skill that you will improve through experience. You often have to think like a compiler. Think about the rules of the language and why it might be throwing an error in that particular part of the code.
If the location of the error is
NOT immediately obvious and you can't see a problem on the line before, then start by taking a look at the top-level item in the error message.
So, in this case, the top-level item was the
getBet
function.
The compilers error message said that the error was occurring inside the
getBet
function.
It couldn't possibly be a problem with the definition of your
analyzeHand
function, as per the line of code it highlighted. Syntactically, that line is correct. So it must be a problem with the getBet function, somewhere at the end.
But as we saw above, the problem was actually the mis-matched braces in the function above it. The
do..while
didn't have a
while
AND the function was not closed properly either. So it was actually sort-of two problems, NOT one!
But either way, those problems lead to a compiler error.
Again, accurately interpreting error messages from compilers is a skill that you'll pick up in time.
As an experienced programmer, the error message you posted told me pretty much everything I needed to know.
Seeing that the error message said that the error was inside your
getBet
function
and that the line highlighted by the error message was the start of your
analyzeHand
function's definition:- That immediately told me, without even looking at the code - that your
getBet
function had not been terminated properly with a closing brace.
The only remaining problem to solve was why the compiler expected a
while
statement. The first thing that came to mind was a
do
loop without a
while
condition at the end.
The next thing I did:
I copy/pasted all of your code into vim and ran a quick gg=G command to auto-indent/reformat all of the code.
Then I immediately saw that there were some imbalances in the braces and balanced them out myself.
And sure enough
getBet
, was NOT terminated correctly. So I fixed that. And immediately saw that there was an unterminated
do
loop. Confirming my suspicions why the compiler was complaing about a missing
while
Problem solved!
After that, I had a quick scan-through the rest of the file and identified the other minor problems in the code.
But again, I'm an experienced progammer. I've been doing this for years. When you're starting out- it's a different story.
So I've shared how I would go about solving the problems, if I was in your shoes and unsure why an error was occurring.
And I also shared how I ACTUALLY solved the problem you had, as an experienced progammer.
Hopefully, my explanations all made sense. But if you have any questions, fire away!