Need help with some C code, please.

Discussion in 'Mac Programming' started by Tander, May 20, 2012.

  1. macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #1
    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:

    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:
     
  2. macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #2
    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:
    Code:
        void   FrenchToastForEveryOne ( void )[COLOR="Red"];[/COLOR]
        {
            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:


    Code:
    void HardBoilThem() {
    }
    
    void MakeAnOmlet() {
    }
    
     
  3. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #3
    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.
     
  4. thread starter macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #4
    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
     
  5. macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    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.
     
  6. thread starter macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #6
    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:
     
  7. macrumors 6502a

    Joined:
    Feb 18, 2008
    #7
    You can set XCode to insert the } for you whenever you type { so that every block you begin has an end.
     
  8. thread starter macrumors 6502a

    Tander

    Joined:
    Oct 21, 2011
    Location:
    Johannesburg, South Africa
    #8
    It was already set - but for some reason, it skips this step every now and then I noticed.
     

Share This Page