0

I am trying to get time input like 12:30:00 which is hour:minute:second and then put them in a struct. Im using cin but it only works when i use space instead of colon like 12 30 00. How can i make it work with colon instead of space? Please be as easy as possible im very newbie about this.

An example could be like:

struct time{
    int hour,minute,second;
    long acc_seconds;
}tm;

int main(){
cout <<"Please enter date as HH:MM:SS";
cin >> tm.hour>>tm.minute>>tm.second;
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
hbk
  • 365
  • 3
  • 8
  • 1
    For starters, check out [How to convert a string variable containing time to time_t type in c++?](https://stackoverflow.com/questions/11213326/how-to-convert-a-string-variable-containing-time-to-time-t-type-in-c) –  Mar 25 '15 at 22:28

3 Answers3

3

Use a place holder object to read the ':' into. Read the numbers into the right objects.

char dummy;
cout << "Please enter date as HH:MM:SS";
cin >> tm.hour >> dummy >> tm.minute >> dummy >> tm.second;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

Sometimes the old is better

scanf("%d:%d:%d", &tm.hour, &tm.minute, &tm.second);
hlscalon
  • 7,304
  • 4
  • 33
  • 40
1

you can store time in String and then divide it into hr,min,sec based on position of 2 colon and then store it in 3 integers.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225