Thread: How do I print from one function?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    How do I print from one function?

    Code:
    /*
    This is a simplified outline of my program as it treats the display of
    titles and messages: messages are printed from individual modules.  I want
    to change the program by printing out all titles and messages from a single
    module, calling this function as and when needed during the execution of the
    program.
    I have difficulty in grasping how this might be done: I want to call this
    single print function from different modules and I want to display only one
    particular message at each call to the function.
    I have two questions:
    1.How would I do this by passing a string (character array: char[]) as an
    argument?
    2.How would I use a pointer to do this?
    */
    
    main()
    {
       one();
    
       two();
    
       three();
    }
    
    one()
    {
       printf("\nmessage 1");
    }
    
    two()
    {
       printf("\nmessage 2");
    }
    
    three()
    {
       printf("\nmessage 3");
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Are you looking for something like this:?
    Code:
    void print_something(const char* something)
    {
        printf(something);
    }
    This is exactly the same:

    Code:
    void print_something(const char something[])
    {
        printf(something);
    }
    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    follow up

    Yes, but I want all three messages to be in the print function & I want to print a different message in turn, calling it from its appropriate function (one, two or three).

    clinamen

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How about:
    Code:
    void printme(const char *msg1, const char *msg2, const char *msg3)
    {
      if (msg1)
        cout <<msg1;
      if (msg2)
        cout <<msg2;
      if (msg3)
        cout <<msg3;
    )
    
    ... somewhere else ...
    
    printme ("Message One", "Message Two", "Message Three");
    printme ("Message One", "Message Two", NULL);
    printme ("Message One", NULL, "Message Three");
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    5

    Thanks, I`ll try it.

    Thanks for this Hammer, I`ll try it out.

    clinamen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM