...

View Full Version : Creating/Using of C++ Functions



JaredC
10-01-2009, 05:33 PM
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.

abduraooft
10-01-2009, 05:39 PM
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?

JaredC
10-01-2009, 05:53 PM
Yeah I gathered that, sorry. Noob to the forums. :P

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


#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.

MattNolan
10-01-2009, 06:23 PM
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.

JaredC
10-01-2009, 07:11 PM
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:


#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.

oracleguy
10-01-2009, 08:31 PM
That solution looks fine, good work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum