Thread: Xcode, Mac or Me??? Learner Question

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    3

    Xcode, Mac or Me??? Learner Question

    Hi all, I have looked around on the web all over and can't seem to get any beginner guidance on making C++ work on a Mac through Xcode 4. I am learning from C++ Without fear.. which is an awesome book and has me hooked. But I have come across a few things that I can't get to work.

    The code below tells me that the line:
    Code:
    int print_out (int n) }
    that Functions that differ only in their return type cannot be overloaded..... which I don't understand.

    The other item I can't work out is it is telling me that the sum in return sum is an undeclared identifier. This is the example answer for Exercise 4.1.2.

    Now they are starting to get taxing should I be looking to install Windows 7 onto a partition and code in windows? Does Xcode have too many restrictions to learn on?

    Any help would be most appreciated as I am loving learning C++!

    Code:
    #include <iostream>
    using namespace std;
    
    
    // Function must be declared before being used.
    
    
    void print_out(int n);
    
    
    int main() {
        int n;
        
    cout << "Enter a number and press ENTER: ";
        cin >> n;
        
    print_out(n);
        
        cout << endl;
        
        return 0;
    }
    
    
    // Print-out function.
    // Prints numbers from 1 to n. 
    
    
    int print_out(int n) {
        int i;
        
        for (i = 1; i <= n; i++)     // For i = 1 to n,
            {cout << i << " ";        //   print i
        return sum;}
    }
    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, first off, you have

    void print_out(int n);
    and
    int print_out(int n)

    Clearly, these two differ only by return type. Your declaration and definition do not match up. You should change your declaration at top to

    int print_out(int n);

    As for the second issue... I have no idea why the function is even trying to return "sum" at all. Why should a function that prints out something return a sum?
    Furthermore, there is no variable "sum" declared in the function which is why the compiler is complaining.
    I don't think there is much wrong with the IDE and/or compiler, so you should be fine.

    If you found this code in a book, then I would be wary. If the book continues to throw code at you that won't compile, then you may consider switching book. If such is the case, Accelerated C++ is a good book.
    You may also want to improve your indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    spaghetticode
    Guest
    Both error messages tell you exactly and literally what's wrong.

    For #1 you might want to check the return type of your function declaration against that of your function definition.
    For #2 note that your return statement uses a variable "sum" that has not been declared nor defined anywhere.

    EDIT: Alright, Elysia was faster.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    Thanks both for your analysis.... I was kicking myself when you highlighted the void/int thing!! took easy to miss!.

    Thanks for the pointer on indentation. Will definitely work on it.

  5. #5
    spaghetticode
    Guest
    These things will happen a lot. No way to count times I have been kicking myself so far.

    You should, btw, learn to listen to your compiler. Some of the error messages are not even close as clear-cut as these two here, but once you get an understanding of how your compiler tells you things you will get better and better in spotting errors.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    I will learn to trust the machine! Encouraging response. Many thanks... again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New C learner need help on a syntax question
    By zmarcoz in forum C Programming
    Replies: 5
    Last Post: 01-11-2012, 10:56 AM
  2. New learner Help!
    By 2001beibei in forum C Programming
    Replies: 2
    Last Post: 06-02-2008, 09:06 AM
  3. Help for the learner
    By inmaterichard in forum Game Programming
    Replies: 3
    Last Post: 08-16-2007, 12:55 AM
  4. 2 Noobish Questions from a 2nd Day Learner
    By Bucket in forum C++ Programming
    Replies: 18
    Last Post: 08-25-2006, 01:20 PM
  5. Learner...
    By Hugo716 in forum C++ Programming
    Replies: 13
    Last Post: 04-21-2006, 03:04 PM