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.

Don't hit enter after each input. Enter literally tells the console to make a new line. A simple space will suffice to separate the variables.

Take it in as a string. Then, find the colons and break the string up to get what you need.

To get what you want, you need to read it as a string, and then parse the string. Some example :

string str;
cin >> str; //for example assume user enters 11:22:33
string::size_type hourEndPosition = str.find_first_of(":");
int hours = atoi( str.substr(0,hourEndPosition) );
string::size_type minuteEndPosition = str.find_first_of(hourEndPosition+1,minuteEndPosition);
int minutes = atoi( str.substr(hourEndPosition, minuteEndPosition - hourEndPoisition);
//.similar for seconds

@Red Goose - I wish it were that simple! That places spaces in there that aren't supposed to be there. Thank you though, I never knew that I could do that in the console!

@Fbody - I was just writing about that and have been scearching through my C++ textbooks. Thanks for the confirmation!

@firstPerson - That actually makes a lot of sense to me. I never knew about the atoi (I believe it's ASCII to Integer, at least from what I've read now). For seconds I would just really have to do this:

string::size_type secondEndPosition = str.find_first_of(minuteEndPosition+1,secondEndPosition);

int seconds = atoi( str.substr(minuteEndPosition, secondEndPosition - minuteEndPosition);

I'm going to try this out now and see what happens!

Alright. I have the code working now, it took a little bit of playing around with and a bit of reading on my part. I just thought I would post the working code for anyone who may visit this thread in the future.

int part2(){
	// Ask user to provide the number of hours, minutes, and seconds.
	cout << "\nWelcome to Second Calculator." << "\nEnter the number of hours, minutes, "
		 << "and seconds (in HH:MM:SS format): "
		 << endl;

	//*********************************************************************************************************************
	string str; cin >> str; // Used to find and break apart the hh, mm, and ss from the input string and store their
							// repective values.
	
	string::size_type hourEndPosition = str.find_first_of(":");
	hourToCalc = atoi( str.substr(0,hourEndPosition).c_str() );
	
	string::size_type minuteEndPosition = str.find_first_of(hourEndPosition+1,minuteEndPosition);
	minToCalc = atoi( str.substr(hourEndPosition+1, minuteEndPosition - hourEndPosition-1).c_str() );
	
	string::size_type secondEndPosition = str.find_first_of(minuteEndPosition+1,secondEndPosition); 
	secLeft = atoi( str.substr(minuteEndPosition+1).c_str() );
	//*********************************************************************************************************************
	
	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;

	return 0;
}

>>>string str; cin >> str
You might consider using getline(cin,str) just in case the user might try to enter something like 05 : 25 : 60 . Notice the spaces.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.