Specify Time (C++)

Associate
Joined
28 Jul 2014
Posts
694
Location
Liverpool
I am trying to write a simple console application that will accept a string from the keyboard (representing a time period in the format HH:MM). This string must them be decomposed into two integers for hours and mins before being resolved into total mins (i.e. 02:20 would resolve to 140 mins etc). The resulting calculation should then be printed to the screen.

This is my code so far
Code:
#include <iostream> //include iostream to get access to cin and cout
#include <string> //include string to get access to a string variable.

using namespace std;

class time {
	int Hr, Min;
public:
	void set_values(int);
	void Show(void);
};

int main()
{
	std::string time;
	int hours = 60;
	int min = 60;

	void set_values(int) {
		Hr = Secs / 3600;
		Min = (Secs % 3600) / 60;
	}

	cout << "Specify time (HH:MM) : ";
	cin >> min;

//Specify system pause
system("PAUSE");
return 0;

}

Output I am trying to print
Code:
Specify Time (HH:MM) : 02:20
02:20 = 140 Mins !

Any help would be appreciated, thanks in advance.
 
Associate
Joined
24 May 2011
Posts
262
What do you need help with? (I can see that the code doesn't work, but I'm not going to write it for you as this is probably some sort of assignment)
 
Last edited:
Associate
Joined
24 May 2011
Posts
262
#include <iostream> //include iostream to get access to cin and cout
#include <string> //include string to get access to a string variable.

using namespace std;

int main()
{
int hours = 60;
int min = 60;
char seperator = ':';

cout << "Specify time (HH:MM) : ";
cin >> hours >> seperator >> min;

cout << "Hours : " + to_string (hours) << endl;
cout << "Mins : " + to_string (min);

return 0;
}
 
Associate
OP
Joined
28 Jul 2014
Posts
694
Location
Liverpool

Thanks a lot for the quick reply. It is similar to what I am trying to achieve, and I am getting this output:

Code:
Speicify Time (HH:MM) : 02:20
Hours : 2
Mins : 20

but how would I have it so that the (HH:MM) would convert to minutes like so:

Code:
Specify Time (HH:MM) : 02:20
02:20 = 140 Mins !
 
Associate
Joined
24 May 2011
Posts
262
In my solution the 'hours' and 'min' variables have the hours and minutes entered by the user. You need to do a calculation using these variables to get the desired result.
 
Back
Top Bottom