Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-01-2009, 05:33 PM   PM User | #1
JaredC
New to the CF scene

 
Join Date: Oct 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
JaredC is an unknown quantity at this point
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.
JaredC is offline   Reply With Quote
Old 10-01-2009, 05:39 PM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
We are helpless to an extend as we have http://www.codingforums.com/rules.htm
Quote:
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?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 10-02-2009 at 08:58 AM..
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
JaredC (10-02-2009)
Old 10-01-2009, 05:53 PM   PM User | #3
JaredC
New to the CF scene

 
Join Date: Oct 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
JaredC is an unknown quantity at this point
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.
JaredC is offline   Reply With Quote
Old 10-01-2009, 06:23 PM   PM User | #4
MattNolan
New Coder

 
Join Date: Sep 2009
Posts: 31
Thanks: 2
Thanked 1 Time in 1 Post
MattNolan is an unknown quantity at this point
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.
MattNolan is offline   Reply With Quote
Old 10-01-2009, 07:11 PM   PM User | #5
JaredC
New to the CF scene

 
Join Date: Oct 2009
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
JaredC is an unknown quantity at this point
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.
JaredC is offline   Reply With Quote
Old 10-01-2009, 08:31 PM   PM User | #6
oracleguy
Rockstar Coder


 
Join Date: Jun 2002
Location: USA
Posts: 9,043
Thanks: 1
Thanked 322 Times in 318 Posts
oracleguy is a jewel in the roughoracleguy is a jewel in the roughoracleguy is a jewel in the rough
That solution looks fine, good work.
__________________
OracleGuy
oracleguy is offline   Reply With Quote
Users who have thanked oracleguy for this post:
JaredC (10-02-2009)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:00 AM.


Advertisement
Log in to turn off these ads.