Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have all the inputs on the same line C++

Tags:

c++

input

cin

I was asked to enter an hour and a minute on the same line. But when I enter the hour, it automatically goes to a new line, and I'm only able to enter the minute on the next line. However, I want to enter the hour and minute on the same line with a colon between them. It should look like this

Time: 4:54 

But my code produces this:

Time: 4 

54

cout << "\n\tTime: "; 
cin >> timeHours;
cin.get();
cin >> timeMinutes;
like image 207
notClickBait Avatar asked Dec 08 '25 14:12

notClickBait


1 Answers

You can do that as the following :

cin >> timeHours >> timeMinutes;

according to the documentation :

the user is expected to introduce two values, one for variable a, and another for variable b. Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab, or a new-line character.

like image 98
Vtik Avatar answered Dec 10 '25 03:12

Vtik