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 :

for(x=0;x<=1;x++)
	{
		cout<<"Enter Train Name: ";
		cin>>o1[x].train_name;
		cout<<"Enter Train No.: ";
		cin>>o1[x].t_number;
		cout<<"Enter Days of Operation: ";
		cin>>o1[x].op_days;
		cout<<"Enter Departure Time (HH:Min): ";
		cin>>o1[x].t_dep.hrs>>o1[x].t_dep.min;
		cout<<"Enter Arrival Time (HH:Min): ";
		cin>>o1[x].t_arr.hrs>>o1[x].t_arr.min;
         }

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

Recommended Answers

All 3 Replies

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..???

#include<iostream.h>
int main()
{
 int hours=0,mins=0;
 cout<<"Enter your time here in the format hours first, then minutes : ";
 cin>>hours>>mins;
 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.