Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Creating/Using of C++ Functions

    Hey all,

    I've been working through the E-Book 'C++ Primer Plus: Fifth Edition' and am now doing the Excercises to do with mainly functions.

    Here's the task at hand (sorry if the question is unbearingly easy to you):

    Write a program that asks the user to enter an hour value and a minute value. The main() function should then pass these two values to a type void function that displays the two values in the format shown in the following sample run:

    Enter the number of hours: 9
    Enter the number of minutes: 28
    Time: 9:28


    I'm able to do the I/O things fine. I'm just stumped on how to transfer the variables from main() to the function, especially with type 'void'.

    Any help would be much appreciated, thanks.

    Jared.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,852
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    We are helpless to an extend as we have http://www.codingforums.com/rules.htm
    1.5) No homework assignments - Do not post your entire homework assignment and request that other members do it for you. This is considered cheating, and your thread may even be used by your school to prove your guilt. Now, you may ask for advice or help on a specific aspect of your assignment that you're having trouble with. Use common sense as far as what's acceptable in terms of soliciting help with homework assignments.
    How about posting the code that you have done so far?
    Last edited by abduraooft; 10-02-2009 at 08:58 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    JaredC (10-02-2009)

  • #3
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Yeah I gathered that, sorry. Noob to the forums. :P

    Here's what I have and what I'm confused about.

    Code:
    #include <iostream>
    using namespace std;
    void time(int); //prototype (is this right?)
    
    int main()
    {
        cout << "Enter the number of hours: ";
        int hours;
        cin >> hours;
        cout << "Enter the number of minutes: ";
        int minutes;
        cin >> minutes;
        int total = time() // I don't understand what to use/do here.
        system("pause");
        return 0;
    }
    
    int time() 
    {
    
    }
    I think the comments speak for the self and well, the lack of the rest of the code.

    Cheers.

  • #4
    New Coder
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post
    You should use a void function to input meaning it does not return a value, you also need to pass two integers to it so your prototype should look like

    void print (int,int);

    And your function header should look like:

    void print(int hrs, int mins)

    Read up on passing variables to functions as well as invoking void functions and you should be able to figure out the function call and what should go within.

  • #5
    New to the CF scene
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattNolan View Post
    You should use a void function to input meaning it does not return a value, you also need to pass two integers to it so your prototype should look like

    void print (int,int);

    And your function header should look like:

    void print(int hrs, int mins)

    Read up on passing variables to functions as well as invoking void functions and you should be able to figure out the function call and what should go within.
    Thanks for the reply Matt,

    I took what you said and applied it in my code here:

    Code:
    #include <iostream>
    using namespace std;
    void print(int,int); //prototype
    
    int main()
    {
        cout << "Enter the number of hours: ";
        int hrs;
        cin >> hrs;
        cout << "Enter the number of minutes: ";
        int mins;
        cin >> mins;
        print(hrs,mins);
        system("pause");
        return 0;
    }
    
    void print(int hrsOut, int minsOut) 
    {
        cout << "Time is: " << hrsOut << ":" << minsOut << endl;
    }
    It works nicely.

    I'm not sure if this is considered wrong in any way (that meant, that I could've done it in a better way).

    Thanks.

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    That solution looks fine, good work.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    JaredC (10-02-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •