Program Closes After Input

I am creating a program that turns a time in seconds into elapsed time (For example, it would change 600 seconds to 00:10:00.) Every time I input the amount of seconds and hit enter, the program closes. Does anyone know how I can fix this?

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
// ConsoleApplication8.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "windows.h"
#include <iostream>

using namespace std;

int main()
{
	int hours;
	int minutes;
	int seconds;
	int timeTotal = 0;
	cout << "Please enter elapsed time in seconds: ";
	cin >> timeTotal;
	cout << endl;
	hours = timeTotal / 3600;                //Divides total number of seconds by 3600 to get hours
	timeTotal = timeTotal - hours * 3600;	
	minutes = timeTotal / 60;				//Divides remaining number of seconds by 60 to get minutes
	timeTotal = timeTotal - minutes * 60;
	seconds = timeTotal;
	cout << hours;
	if (minutes < 10)						//If there are less than 10 minutes, the number of minutes goes behind :0 to display the time
		cout << ":0" << minutes;
	else
		cout << ":" << minutes;				//If there are more than 10 minutes, the number of minutes goes behind : to display the time
	if (seconds < 10)
		cout << ":0" << seconds;			// If there are less than 10 seconds, the number of seconds goes behind :0 to display the time
	else''
		cout << ":" << seconds;				//If there are more than 10 seconds, the number of seconds goes behind : to display the time
	cout << endl;
	getchar();
	return 0;
}
closed account (48T7M4Gy)
line 30 what is '' ?
@kemort i didn't even realize that was there. I deleted it in my program but it still closes after input.
closed account (E0p9LyTq)
Run the program from a command prompt.

http://www.cplusplus.com/forum/beginner/1988/
closed account (48T7M4Gy)
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
// ConsoleApplication8.cpp : Defines the entry point for the console application.

#include <iostream>

using namespace std;

int main()
{
	int hours;
	int minutes;
	int seconds;
	int timeTotal = 0;
	cout << "Please enter elapsed time in seconds: ";
	cin >> timeTotal;
	cout << endl;
	hours = timeTotal / 3600;                //Divides total number of seconds by 3600 to get hours
	timeTotal = timeTotal - hours * 3600;	
	minutes = timeTotal / 60;				//Divides remaining number of seconds by 60 to get minutes
	timeTotal = timeTotal - minutes * 60;
	seconds = timeTotal;
	cout << hours;
	if (minutes < 10)						//If there are less than 10 minutes, the number of minutes goes behind :0 to display the time
		cout << ":0" << minutes;
	else
		cout << ":" << minutes;				//If there are more than 10 minutes, the number of minutes goes behind : to display the time
	if (seconds < 10)
		cout << ":0" << seconds;			// If there are less than 10 seconds, the number of seconds goes behind :0 to display the time
	else
		cout << ":" << seconds;				//If there are more than 10 seconds, the number of seconds goes behind : to display the time
	cout << endl;
	getchar();
	return 0;
}


I deleted the '' and the first two lines and it runs ok in the cpp shell - press the gear wheel at top RH corner. I haven't checked the arithmetic but it looks OK.


... just checked for a few values and sums are are OK. :)
Last edited on
Topic archived. No new replies allowed.