PDA

View Full Version : Need help with some C code, please.




Tander
May 20, 2012, 04:35 AM
Hey all,

So I am busy reading "Learn C on Mac" - I'm on chapter six at the moment.

Busy learning about the switch statement and I understand exactly how it works. So I wanted to make a small program - which uses the number of eggs to decide what statement it should execute.

I also decided instead of a simple printed () statement, I would ask the compiler to call a function instead.

So here is the code:


#include <stdio.h>


void HardBoilThem (void);
void MakeAnOmlet (void);
void FrenchToastForEveryOne (void);


int main (int argc, char const * argv[])

{
int numberOfEggs;

numberOfEggs = 12;

switch ( numberOfEggs ) {
case 1:
case 2:
HardBoilThem();
break;
case 3:
MakeAnOmlet ();
break;
default:
FrenchToastForEveryOne();

return 0;

}

void FrenchToastForEveryOne ( void );
{
printf ( "French Toast!\n" );


}


Now I am aware that I have three functions that have no definitions - I was first testing the "French toast" function.

I have rewritten the code for all functions to have definitions and xCode gives me the following for errors:

Undefined symbols for architecture x86_64:
"_HardBoilThem", referenced from:
_main in main.o
"_MakeAnOmlet", referenced from:
_main in main.o
"_FrenchToastForEveryOne", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Now I am not 100% sure what this is telling - but it seems like it is not referencing the functions?

Any help to understand this code would be great!

Thanks guys. :cool:



McGordon
May 20, 2012, 05:16 AM
First of all, there's a missing } at the end, but that might have been a copy/paste error when you posted it here.

From your indentation and missing } after main, it looks like you're defining FrenchToastForEveryone inside main. You also have a semicolon after it, separating it from its definition:
void FrenchToastForEveryOne ( void );
{
printf ( "French Toast!\n" );


}

After you've fixed that, you'll still have 3 errors:
Undefined symbols for architecture x86_64:
"_HardBoilThem", referenced from:
_main in main.o
"_MakeAnOmlet", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is just because you haven't defined those functions. Your declarations at the top are fine for forward referencing a function so you can use it in main if its defined further down, like your FrenchToastForEveryone function. The other two aren't defined and that's what its complaining about. You'd be better off putting empty functions and fill them out as you go, like:


void HardBoilThem() {
}

void MakeAnOmlet() {
}

gnasher729
May 20, 2012, 05:19 AM
Hey all,

So I am busy reading "Learn C on Mac" - I'm on chapter six at the moment.

Busy learning about the switch statement and I understand exactly how it works. So I wanted to make a small program - which uses the number of eggs to decide what statement it should execute.

I also decided instead of a simple printed () statement, I would ask the compiler to call a function instead.

So here is the code:


#include <stdio.h>


void HardBoilThem (void);
void MakeAnOmlet (void);
void FrenchToastForEveryOne (void);


int main (int argc, char const * argv[])

{
int numberOfEggs;

numberOfEggs = 12;

switch ( numberOfEggs ) {
case 1:
case 2:
HardBoilThem();
break;
case 3:
MakeAnOmlet ();
break;
default:
FrenchToastForEveryOne();

return 0;

}

void FrenchToastForEveryOne ( void );
{
printf ( "French Toast!\n" );


}


Now I am aware that I have three functions that have no definitions - I was first testing the "French toast" function.

I have rewritten the code for all functions to have definitions and xCode gives me the following for errors:



Now I am not 100% sure what this is telling - but it seems like it is not referencing the functions?

Any help to understand this code would be great!

Thanks guys. :cool:

1. What you posted will not compile. There's a closing parenthesis missing. As a result, I have to guess what your actually code is.

2. You cannot write one function (FrenchToastForEveryone) inside another function (main).

3. Assuming there is a closing parenthesis at the end, you didn't actually write one function inside another. The ";" after FrenchToastForEveryone (void) means this is just a repeated declaration of the function.

4. The linker tells you that three functions are missing. You need to write these functions before you can compile and run the program. Even if they just have an empty body.

Tander
May 20, 2012, 05:50 AM
The missing curly brace was an error on my part - I never intended for the function to work inside of main, as I know better than that. It was simply a lack of attention to my code and missing the fact that I needed a close curly brace!

The ' ; ' was due to xCode moaning about it missing - but obviously it was confused as my code would not compile.

So I didn't make a complete mess of the program then?

Appreciate the help guys!

Re-wrote the code and it now works as intended. I also learnt to pay more attention to those curly braces! :D

larswik
May 20, 2012, 11:25 AM
As a habit I got into a while back is to add both of my {}'s at the same time. Then back up a few space and start to write my code between them. That way you are sure not to forget it since you already added it.

But the way you are approaching your learning is good, like I did. Take what you are learning, come up with your own ideas and write new code.

Reading the book is good. Doing the tutorials is better. Creating your own projects from the 2 is best.

Tander
May 20, 2012, 11:47 AM
That's a good way to do it, actually! :D

Thanks for the tip, I will remember that.

I find doing my own projects keeps it fresh and makes it interesting for me to learn.

:cool:

knightlie
May 21, 2012, 01:56 AM
You can set XCode to insert the } for you whenever you type { so that every block you begin has an end.

Tander
May 21, 2012, 03:05 AM
You can set XCode to insert the } for you whenever you type { so that every block you begin has an end.

It was already set - but for some reason, it skips this step every now and then I noticed.