hh:mm in input?

how can i write time in this format hh:mm in input?
for example
00:42
Last edited on
any answer?
which input prevents you from writing so?
i write like that but it gives error cin>>a>>":">>b;
What do you expect that line to do? You are trying to put user input to a defined string (meaning that ":" is not a variable which you can assign stuff to as it is already defined as ":"). If what you are trying to do is print the variables and the colon, use cout<<.
closed account (10oTURfi)
hehe
This is what you want to do:
1
2
3
int hh, mm;
cin >> hh >> mm;
cout << hh << ":" << mm;
Krofna wrote:
This is what you want to do:
Nope, he want his input formatted.

@TURAL MeLIKLI
cin doesn't allow formatted input. You have to read it as a string and check whether it's ok or not
for example user enter this 12:54 is it possible without string?
You can use scanf http://www.cplusplus.com/reference/clibrary/cstdio/scanf/ like so:

scanf("%02d:%02d", &i, &j);

that'd work only if the user inputs the right combination. Like any other stdandard console input function it doesn't constrain the user input anyway
you can create a time class with hour and min var and overload the '>>' cin as a public friend

1
2
3
4
5
6
7
friend istream &operator>>( istream &input, Time &right ) //input in hh:mm format
{
        input >> right.hour; // input hours
        input.ignore(); // skip :
        input >>right.min; // input minute part
        return input;
}
Topic archived. No new replies allowed.