Issue with a program that calls a function and has a loop

I'm taking a intro course to C++ and this is a program for one of our assignments. I corrected the errors but the program just won't run. I think I just completely trashed the loop. I've included the instructions as the what was supposed to be included and what the program was supposed to do. can someone help me out and see what I did wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Create and run a program that includes a main and a function. The function name should be convert_to_secs. the function receives an integer representing seconds, an integer representing minutes and an integer representing hours. The function determines the total number of seconds and returns the results to the caller.

The main program should have a loop to perform conversion 3 times ( loop to be performed 3 times ). Inside the loop :

1. prompt the user for seconds, minutes and hours.

2. call the function to perform conversion.

3. inform the user of the result.

Run the program. Provide your own data.

Copy the output from the output window and imbed at the bottom of the program before submitting. 

Student Response:	******I worked on this program and I got the errors cleared but I messed up somewhere as the program didn't work. Here is what I have.********** 

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream> // Robert Sutherland CPT 234 Test 3
using namespace std; 

int convert_to_secs(int minutes, int hours);

int main()

{
int convert_to_secs(int minutes, int hours);

int seconds;

while (seconds > 60)
{
int seconds;
int minutes;
int hours;
int total_converted_minutes;
int total_converted_hours;

cout << "Please enter in a number of seconds: ";
cin >> seconds;
cout << "Please enter in a number for minutes: ";
cin >> minutes;
cout << "Please enter in a number for hours: ";
cin >> hours;

int convert_to_secs(int minutes, int hours);

if(seconds >= 61)


cout << "The number of seconds is " << seconds;

return 0;



if (minutes > 1)


int total_converted_minutes;

cout << minutes << " eqaul " << total_converted_minutes << " seconds ";

return 0;




if (hours > 0)

int total_converted_hours;

total_converted_hours= 3600*hours; //there are 3600 seconds in 1 hr

cout << hours << " equals " << total_converted_hours << " seconds ";

return 0;

}
}

int convert_to_secs(int minutes, int hours)
{
int total_converted_minutes;
int total_converted_hours;

total_converted_minutes= 60*minutes;

total_converted_hours= 3600*hours;

return 0;
} 
  
You're making a few mistakes here...

First of all, you got the princips of scope wrong. If you declare a variable inside a function, that variable is only inside that function (local scope). Of course, you can declare a variable with the same name in another function, but that is a different variable (holding a different value).
You can also declare a variable outside any function, then it has global scope, but you should avoid this as much as possible (it gets confusing in bigger programs)

Second, you're using a lot of 'return' statements. A normal function has a type, and returns a value of that type (you can also declare a function as 'void', wich means it doesnt return anyting). It is a rule that function main() always returns an integer value, 0 if the program was executed without problems, and another value if there is an error. In simple programs as this one, main returns only once (the same for all other functions).
When using a normal function, you can store the value it returns in a variable, or display it, or use it in a condition, or whatever.

Beside those general points, you got a wrong logic behind this program. You should prompt the user for seconds, minuts and hours, then pass those variables to the function convert_to_secs (int seconds, int minutes, int hours) and store the value this function returns (the total amount of seconds) in a variable and display his value (or display the returnvalue directly).

Hope this helps, if you got any questions, please ask
Last edited on
This is in no way correct but i thought i would fix some of your code up so you can read it a bit better. Yes this does work though point being I'm not doing all your code for you I'm just showing you the program working but you still need to fix how the while loop should work etc.

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
#include <iostream> // Robert Sutherland CPT 234 Test 3
using namespace std; 

int hoursToSeconds(int iHours)
{
	iHours *= 60 * 60;
	return iHours;
}
int minutesToSeconds(int iMinutes)
{
	iMinutes *= 60;
	return iMinutes;
}

int main()

{
	int seconds;
	cout << "Please enter in a number of seconds: ";
	cin >> seconds;

	while (seconds > 60)
	{
		int minutes;
		int hours;
		int total_converted_minutes;
		int total_converted_hours;

		cout << "Please enter in a number for minutes: ";
		cin >> minutes;
		cout << "Please enter in a number for hours: ";
		cin >> hours;

		total_converted_minutes = minutesToSeconds(minutes);
		total_converted_hours = hoursToSeconds(hours);

		if(seconds >= 61)
		{
			cout << "The number of seconds is " << seconds << endl;
		}

		if (minutes > 1)
		{
			cout << minutes << " Minutes equals " << total_converted_minutes << " seconds " << endl;
		}

		if (hours > 0)
		{
			cout << hours << " Hours equals " << total_converted_hours << " seconds " << endl;
		}

	}
}


So now you see that it does work and will run - so now edit the while loop for yourself to make the program correct.
Thanks Scipio and Mythios!
No problem - glad to help
Topic archived. No new replies allowed.