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?
// ConsoleApplication8.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "windows.h"
#include <iostream>
usingnamespace 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;
}
// ConsoleApplication8.cpp : Defines the entry point for the console application.
#include <iostream>
usingnamespace 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. :)