I am currently writing a program that can calculate the number of total seconds in a user given number of hours, minutes, and seconds (in that order in hh:mm:ss format hh, mm, and ss being any number from 0-99). I'm not really worried for checking for errors in user input, I'm more concerned with the user inputing code into the console in that format (hh:mm:ss).
Example:
Enter time in format hh:mm:ss: 2:15:30
Number of seconds is 8130
This is my program so far:
#include <iostream>
using namespace std;
int secToCalc; int minToCalc; int hourToCalc; int secLeft; int minLeft; int hourLeft;
// Take the input from the user and calculate the number of seconds through
// multiplication. 3600 sec = 1 hour AND 60 sec = 1 min
int main(){
// Ask user to provide the number of hours, minutes, and seconds.
cout << "Welcome to Second Calculator." << endl << "Enter the number of hours, minutes, and seconds (in that order): "
<< endl;
cin >> hourToCalc >> minToCalc >> secLeft; // Get hours, minutes, and seconds from user.
int secTotal = (hourToCalc * 3600) + (minToCalc * 60) + secLeft; // Formula for calculating total number of seconds.
// Display the calculated nummber of seconds from user given hours, minutes, and seconds.
cout << "From the given " << hourToCalc << ":" << minToCalc << ":" << secLeft
<< " the total number of seconds is equal to: " << secTotal << endl;
system("pause"); // Prevent console from closing immediately, press enter key to close console
return 0;
}
The output currently looks something like this:
Welcome to Second Calculator.
Enter the number of hours, minutes, and seconds (in that order):
2
15
30
From the given 2:15:30 the total number of seconds is equal to: 8130
Press any key to continue...
I've looked around and from what I have read I would need to move the cursor in the console, but that's a tad advanced for what little I've learned so far.
I'm not sure if maybe I have to do something with some string operations and somehow find and store the three values from the user input. I've tried outputing ":" after each input, but the console places new lines after each input and it just ends up looking like a wierd ladder:
2
:15
:30
Any help in steering me in the right direction or assisting in how to figure this out is greatly appreciated.