hey guys,

i am trying to make a program that overloads the istream with which you can create an object by asking from the user to enter values and you can use cin>>name_of_the_object to create it.
My question is how can i ask the user to enter a certain hour of his choice e.g.
You must assume that the user will input for each value (hour,minutes,seconds) a two-digit value and the value will contain the two colons as seen in the example.
When the object will be created the colons will be ignored.

Thanks for your time.

6 Years
Discussion Span
Last Post by jonsca

Your question is a little fuzzy -- what kind of object do you want to create when the time is entered? Are you talking about a clock?


My question is how can i ask the user to enter a certain hour of his choice e.g.

cout << "Enter a time in the format hh:mm:ss -- " << endl;
cin >> GetTime;

thanks for the reply but the problem is how am i going to write the proper code in order to recognize the users input in the format e.g. 15:45:21 and ingore the : colon hence assign the values of hour, minutes and seconds in the proper variables.

In other words, the user is going to be asked to input a certain time e.g. 15:45:21
With the use of an overloading istream function i have to recognize from this input that contains the colons and seperate hours, minutes and seconds in order to assign them in the appropriate object set methods.

i hope i was clear.


Yes, you were clear, but I'm trying to nudge you in the right direction.

This was my suggestion:

Scan the string the user enters up until you hit a ':' then convert and repeat.

The case you are looking to avoid is where there is a single digit time (e.g., 9:03:04) because you only have 1 digit, so you need to account for that.

A std::string can be used like an array of characters.

To obtain the integer associated with a number character, subtract '0' from it (e.g., '9' - '0' = 9)

You know the place values of each of those digits so you can get their value and store it as an int.

See if that doesn't get you a bit farther and post back with your attempt.


i thought about creating an array as well but the thing is that i am using istream overlading to assign values, which means that in my driver i will ask for the user to write a time my code will be cin>>t4;

In my function definition i will have the following code:

istream& operator>>(Time &t)
  return in;

How will i assign the array values in that function?i got confused because i dont know how to manipulate streams.
If there is a way to obtain the value that is inputted by the user (only with the istream overloading) and assigned in an array i can declare a char array and perform a[0]+a[1](example only for hour) which will give me two numbers together.This numbers can be converted into int through a to i function and then somehow be assigned to the object to form the time inputted.

Any ideas how i can change the overloading function?

Keep in mind that i consider that the user will input the time in the correct format hh:mm::ss


Check into using getline with a ':' delimeter, otherwise you can do it character by character like I was suggesting before, say the user enters 12:05:15 =>

Add '1' to a temporary string, count =1, add '0' to the temporary string, count = 2. We've reached a colon, note it and stop counting.

Convert the characters to numbers and add their place values:

subtract '0' from '1' to get 1, then *10^(count-1) = 10
subtract '0' from '2' to get 2, then *10^(count-2) = 2
Add them to get 12

Scan up to the next colon doing the same thing. The advantage of your situation is that you know that there are only 2 digits in between colons, so your multiples of 10 are always going to be the same.

Edited by Nick Evan: Replaced CODE with ICODE tags. (using ugly hack to avoid syntax highlighting on pseudocode)

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.