storing variables function

I'm not sure how to really call the function Seconds and store the result in two different variables clock_one and clock_two. The user is supposed to input the information requested, then the program goes to the function Seconds and stores the info in the variable clock_one and then goes back to main and prompts the user for the same info but different numbers, goes to Seconds again and then the info is stored in clock_two. I'm sure I'm supposed to use return but I dont quite know how.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <iostream>
using namespace std;

int main()
{
	int hours;
	int minutes;
	int seconds;
	int clock_one;
	int clock_two;
	int seconds_apart;

	cout << "Input first clocktime...";
	cout << "Hours: ";
		cin >> hours;
	cout << "Minutes: ";
		cin >> minutes;
	cout << "Seconds: ";
		cin >> seconds;

		Seconds(hours, minutes, seconds); //send to function

	cout << "Input second clocktime...";
	cout << "Hours: ";
		cin >> hours;
	cout << "Minutes: ";
		cin >> minutes;
	cout << "Seconds: ";
		cin >> seconds;

		Seconds(hours, minutes, seconds); //send back to function

	cout << "It's been " << clock_one << " since the first clock struck 12:00";
	cout << "It's been " << clock_two << " since the second clock struck 12:00";

	if (clock_one > clock_two)
	{
		seconds_apart = (clock_one - clock_two);
		cout << "The two times are " << seconds_apart << " seconds apart.";
	}
	else
	{
		seconds_apart = (clock_two - clock_one);
		cout << "The two times are " << seconds_apart << " seconds apart.";
	}

	
	return 0;
}

int Seconds(int hours, int minutes, int seconds)
{

	hours*(3600) + minutes*(60) + seconds;
	return ();
}
closed account (jvqpDjzh)
Do you have to save the information inside the functions seconds? Should this information remain (from the last call to the function) the same inside the function until we don't call the function again?

EDIT: Can you use other functions than Seconds?
Last edited on
yes, I can use other functions but the professor said it's not necessary.

The way I understand it, the info has to go through Seconds and then go back to main and be stored in either clock_one or clock_two.

I've added numseconds in the hopes that I can store it in clock_one and clock_two depending on the user input.


1
2
3
4
5
6
7
8
int Seconds(int hours, int minutes, int seconds)
{
	int numseconds;

	numseconds=( (hours*(3600)) + (minutes*(60)) + (seconds));
	return (numseconds);

}
These are the instructions:

Write a function called Seconds that takes in three integer parameters (representing hours, minutes, and seconds) and returns the number of seconds since the last time the clock "struck 12" (i.e. was at 12:00:00 -- AM or PM doesn't matter since you're not tracking this).

To test this function, write a main() routine (in the same file) that prompts the user to enter hours, minutes, and seconds for two different clock times; then uses the Seconds function to calculate the shortest amount of time in seconds between the two times (both of which are within one 12-hour cycle of the clock); the print out the number of seconds since "striking 12" for both clocks, as well as the number of seconds between the two clock times.
closed account (jvqpDjzh)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
int Seconds(int, int, int);
int main(int argc, char** argv) 
{
	int clock_one = 0;
	int clock_two = 0;
	
		cout << "Input first clocktime...";
	cout << "Hours: ";
		cin >> hours;
	cout << "Minutes: ";
		cin >> minutes;
	cout << "Seconds: ";
		cin >> seconds;

	//(1)
	//calling the function Seconds, that calculates the time in seconds and returns it
	//saving the result in the variable clock_one
	clock_one = Seconds(hours, minutes, seconds);

	cout << "Input second clocktime...";
	cout << "Hours: ";
		cin >> hours;
	cout << "Minutes: ";
		cin >> minutes;
	cout << "Seconds: ";
		cin >> seconds;
		
	clock_two = Seconds(hours, minutes, seconds);//same as point (1)
	/*

        Do what you need with clock_one and clock_two
       */

	return 0;
}

int Seconds(int hours, int minutes, int seconds)
{
	return hours*(3600) + minutes*(60) + seconds;
	//you are returning the result to the function that call Seconds, 
	//which is the main
}

Last edited on
A function can return a value. For example the main function returns 0 because that tells the operating system that the program executed successfully. When you call functions in your program, you might use them in a more complicated expression instead of just having only the function on the line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int max(int a, int b)
{
   if(a > b)
   {
      return a;
   }
   else
   {
      return b;
   }
}

//... somewhere later, perhaps inside int main ...

int falala = 5;
int singing = 10;
int larger = max(falala, singing); //larger should hold a value of 10 now 


Last edited on
thank you so much!!

that solves everything
Registered users can post here. Sign in or register to post.