I am making a project of Railway reservation system. I want to input time of Train arrival and departure as hours and minutes. Now i want both of them to be entered in same line, first hours and then minutes but in same line i.e the cin should not go in second line after i input the hours. Both hours and minutes should be entered in same line without a line break. I am making this program using structures, here's the small code snippet of the project :

		cout<<"Enter Train Name: ";
		cout<<"Enter Train No.: ";
		cout<<"Enter Days of Operation: ";
		cout<<"Enter Departure Time (HH:Min): ";
		cout<<"Enter Arrival Time (HH:Min): ";

Please help how can i Cin two values in same line without a line break after first input stream..???

8 Years
Discussion Span
Last Post by WaltP

I don't use cin very often the easy solution would be to read in your input as a string variable and then parse the line.

So you could take in a string
and find a char say ':' and then you would have to convert the two sub strings into numbers.

but when playing with user input it is generally best where you can to get the user to adhere to a fixed rule and stick to it. The timesz when you would not do this is if you are dealing with information from a fixed source.

I am pretty sure that the operator >> could be overloaded to interpret reading into a time element and interpretting a single input and this probably already exists for date - time. You would use a class instead of a struct.

Please help how can i Cin two values in same line without a line break after first input stream..???

int main()
 int hours=0,mins=0;
 cout<<"Enter your time here in the format hours first, then minutes : ";
 cout<<"Your time is "<<hours<<" "<<mins<<endl;
 return 0;

Read the input as a string and test each character to make sure it's in the proper format nn:nn . Then convert the numbers.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.