Reputation: 25
I need to take a users input such as 10:35 and put it into a variable of hours and minutes. How can I do this using the colon as the separate for the two? This is how the assignment requires it be entered.
Example of what I am doing.
int main()
{
char again = 'y';
int userHours = 0;
int userMinutes = 0;
while (again == 'y')
{
cout << "Enter a time in 24-hour notation: ";
cin >> userHours >> ":" >> userMinutes;
}
return 0;
}
Upvotes: 0
Views: 2731
Reputation: 15916
You almost had it:
#include <iostream>
int main()
{
int hours, minutes;
char colon;
std::cin >> hours >> colon >> minutes;
std::cout << hours << " : " << minutes;
return 0;
}
Upvotes: 0
Reputation: 692
input and convert it to string , then split it into 2 strings , and then convert them in integers at last.
Upvotes: 1