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
  • 59,144
  • 17
  • 92
  • 149
hbk
  • 315
  • 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
  • 200,579
  • 13
  • 144
  • 260
2

Sometimes the old is better

scanf("%d:%d:%d", &tm.hour, &tm.minute, &tm.second);
hlscalon
  • 7,100
  • 4
  • 30
  • 39
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
  • 33,155
  • 24
  • 179
  • 178