Can someone check my solution?

Problem : Write a problem that asks a user to enter a number of seconds. The program will then use the entered number to convert it to Days, Hours, Minutes and Seconds. Then the program will spit out the information. Use symbolic constants to represent the number of hours in a day, the number of minutes in an hour, and the number of seconds in a minute.

The program seems to function but I'm not sure if I did the "symbolic constant" part correctly..

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
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    
    long int seconds;
    cout << "Please enter the number of seconds: ";
    cin >> seconds;
    cout << endl;
    
    const int Khours_per_day = 24;
    const int Kminutes_per_hour = 60;
    const int Kseconds_per_minute = 60;
    
    long int days = seconds / Kseconds_per_minute / Kminutes_per_hour / Khours_per_day;
    long int hours = seconds / Kseconds_per_minute / Kminutes_per_hour % Khours_per_day; 
    long int minutes = seconds / Kseconds_per_minute % Kminutes_per_hour;
    long int sec = seconds % Kseconds_per_minute; 
    
    cout << seconds << " seconds = " << days << " days " << hours << " hours " << minutes << " minutes and " << sec << " seconds" << endl;
    
    
    cin.get();
    cin.get();
    return 0; 
}
Yes you did

edit:
a tip: having two cin.get()s is kind of silly. If you change your code, you may need to press enter twice. You have them there because cin>> leaves a newline char in the input stream. To solve this you need to remove it. cin.ignore does that. The right way would be to #include <limits> and use cin.ignore(numeric_limits<streamsize>::max(), '\n') but if you find this very confusing a cin.ignore(/*any big number*/256, '\n') would do..
Last edited on
Topic archived. No new replies allowed.