Easy Peasy Function Problem

This is a discussion on Easy Peasy Function Problem within the C++ Programming forums, part of the General Programming Boards category; Just starting to look at functions and i wanted to write one that would go on to a new line. ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    Question Easy Peasy Function Problem

    Just starting to look at functions and i wanted to write one that would go on to a new line. I know this code is really impractical but if you know what i am trying to do would you be a pal and show me the way. The less you change my code the more help it will be to me.

    // This is my first function (no ........ sherlock)

    #include <iostream.h>

    void nline(void); // function prototype

    int main()
    {



    cout << "Hello"
    << nline(void)
    << "Kiddies"
    << nline(void)

    return 0;
    }

    void nline(void) // newline function
    {
    cout <<endl;
    return ;
    }

    Any other constructive help about functions would be appreciated.
    (an url maybe)

    Thanks

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You don't need the << before the nline() function -

    cout << "Hello" ;
    nline();
    cout << "Kiddies";
    nline();


    You should either use '\n' or endl, it would make your life easier.

  3. #3
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Zen can you somehow return on ostream object though?

    Would you have to overload the '<<' operator?
    Last edited by Witch_King; 08-31-2001 at 07:06 PM.
    I compile code with:
    Visual Studio.NET beta2

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Zen you are a star!

    I know the program was of sod all use but it was the easiest function i could think to write so the fact that i could'nt do that was some what disturbing.

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Aww, come on, no answer? Oh well.
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes but you can only overload the << operators for user defined types, not primitives or functions. It works like this -

    Code:
    #include <iostream>
    
    using namespace std;
    
    class t
    {
    public:
        int i;
    };
    
    ostream& operator<< (ostream& os,t test)
    {
        return os<<test.i;
    } 
    
    
    int main()
    {
        
        
        t test;
        test.i=10;
    
        cout << test << endl;
    
        return 0;
    }
    

    If you wanted to return an ostream just to create a newline , I think you'd have to still call a function -

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    ostream& nline(ostream& os)
    {
        return os<<"\n";
    }
    int main()
    {
        
        nline(cout);
    
        return 0;
    }
    

  7. #7
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    So you can't return a manipulator in a compound cout statement? Thanks, that code helps.
    I compile code with:
    Visual Studio.NET beta2

  8. #8
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If by compound statement you mean chaining, then you can do that. That's why the ostream is returned rather than just outputing the type in the function.

    Code:
    #include <iostream>
    
    using namespace std;
    
    class t
    {
    public:
        int i;
    };
    
    ostream& operator<< (ostream& os,t test)
    {
        return os<<test.i;
    } 
    
    
    int main()
    {
        
        
        t test;
        test.i=10;
    
        t test2;
        test2.i=20;
    
        cout << test << endl <<test2<< endl;
    
        return 0;
    }
    

  9. #9
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Yeah I can see how a copy of the class data is returned and I guess the manipulator '\n' should just be called with 'endl' rather than trying to return it with a function that is part of a 'cout' chain.

    This was confusing at first though:
    Code:
    ostream& operator<< (ostream& os,t test)
    {
        return os<<test.i;
    }
    Because I would have thought that 'ostream& os' was not a parameter since 'ostream&' was a return value. But than again i'm not used to working with C++ streams.
    I compile code with:
    Visual Studio.NET beta2

  10. #10
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    There's nothing stoppping you returning an endl or '\n' included in the ostream but when overloading operators it's best to imitate the default behavior as when you pass an int into an ostream -

    int i=0;
    cout << i;

    it doesn't get an endl or '\n' tagged onto the end of it.

    It's up to the user(programmer) to decide if they want to put one on and you may have other functions that rely on there not being a newline.

  11. #11
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    Code:
    ostream& operator<< (ostream& os,t test)
    {
        return os<<test.i << endl;
    }
    Actually this does work. Yes true ofcourse there is no need to do this, that wasn't what I was after. Anyway this is good.
    I compile code with:
    Visual Studio.NET beta2

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob problem - function call
    By Ultraman in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2006, 03:28 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 07:49 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21