Thread: function question

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    3

    function question

    if i have information that is coming from a function

    such as a cout << "InFO"

    and i want to just display that as another function.

    (I.E. instead of having the cout , i want to just put the function ther einstead...and hav ethe function read my cout...how would i go about doing that..


    else if (index != NOT_FOUND)
    {
    UpdateCounter (list.counters[index], count, weight);


    cout << "Counter " << name << " updated with " << count;
    cout << " packages " << "weighing " << weight ;
    cout << " lbs." << endl;


    for example...i would replace all the cout with a function

    my coding is not right i dont think...any suggestions

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I'm not sure I understand what you want to do...

    Do you want a function that has the couts inside it?

    like this?-
    Code:
     
    MyPrintFunction(name, count, weight)
    {
    cout << "Counter " << name << " updated with " << count; 
    cout << " packages " << "weighing " << weight ;
    cout << " lbs." << endl;
    }
    You should be able to do this after reading the Function Tutorial.

    You also need to understand how to pass a pointer-to-a-string into a function (I assume that name is a pointer to character array (AKA a C-style string).

    Don't forget to include:
    The function prototype
    The function definition
    One or more function calls

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    "cout << " is actually two entities, a class and a operator overload, so to replace cout with your own "function", you'd need to create a class to be the recipient and overload operator<< to return that class. Additionally, you'll have to create an overloaded operator<< for every type you expect to be able to input to your class;

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Darryl
    "cout << " is actually two entities, a class and a operator overload, so to replace cout with your own "function", you'd need to create a class to be the recipient and overload operator<< to return that class. Additionally, you'll have to create an overloaded operator<< for every type you expect to be able to input to your class;
    ...uhhhm, I don't think you've sized up your audience accurately.
    Last edited by 7stud; 04-25-2005 at 08:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM