General

Chat

C help again

EDIT : C++ into the title sorry.
Sorry to bother again. I'm typing up a program right now and it requires me to input a departure and arrival time. How can I make it so that the input value has to be within 0:00 and 23:59?

Right now I have

http://pastebin.com/6K4VjJjx

and I want to make it so that they can't be a time outside of the boundaries.

March 13, 2014

11 Comments • Newest first

ehnogi

@YumToast:

Yeah I posted that facetiously, then OP took the time to send me a PM for help, so I told him to convert his data type to use strings (I asked him what he was using and he said double... which would work if you like to write an unnecessary amount of code) and use the find.str function and set parameters of the portions of the string.
He could actually use substrings if he wanted. Would probably be more efficient actually.

I'm not going to write out the code and do someone else's homework.
This isn't cplusplus.com

Reply March 13, 2014 - edited
Kuraitou

std::getline to read a line of input.
std::string::find to see if said line contains a ':' character.
If it does, you can use std::string::substr to extract the hours and minutes, then convert them to integers using std::stoi (C++11) or std::atoi. Otherwise the user didn't input a valid time.
Then you can do bounds checking on the hours/minutes.

I assume this is homework so I won't write a full implementation for you. You can look up all these functions on [url=http://en.cppreference.com/w/]cppreference.com[/url] using the search bar in the top right.

Reply March 13, 2014 - edited
ehnogi

@MarshMallows:

Strings are the only way this code could work.

I mean, you could use arrays if you want to keep it as a double (the OT was planning on using double) but it's even more inefficient. I don't remember if double registers ":" either so you'd have to use two data types when you could just use a string.

Reply March 13, 2014 - edited
Yumtoast

[quote=bloodIsShed]^are you on a vpn or a proxy by any chance? iirc someone had problems using basil because of that[/quote]
No, I'm on my home network.

It's pissing me off so much that I'm leaving incredibly negative messages on the "leave feedback" portion of the CAPTCHA page.

Reply March 13, 2014 - edited
bloodIsShed

^are you on a vpn or a proxy by any chance? iirc someone had problems using basil because of that

Reply March 13, 2014 - edited
Yumtoast

ehnogi's method would never work.
bloodIsShed's method is the most convenient, but your instructor may not allow for 2 inputs. [b]If possible, use his implementation.[/b]
Arios' method works only if the delimiter doesn't consume letters, which it will (see below).

I'm assuming the user-input is a string and you can't ask the user for 2 separate inputs for minutes and hours, so here's my pseudo implementation (it looks intimidating, but it's less than 30 lines of code):

1) Use the replaceall function to replace occurrences of ':' with "" (empty string); alternatively, you can use ^[0-9] as a delimiter and remove all instances of non-numerical characters. The replaceall method will work better, as the delimiter will strip the input of letters (e.g. if the input x12:00 was used, it would result in 1200 and accept the input, when it clearly shouldn't). If the replaceAll method doesn't replace anything, that means there are no colons and the input should be rejected.

Special case: If your input has multiple colons (e.g. 2:3:0), then you might just want to go with the replace method that replaces only the first instance of a colon.

2) Does C++ have a method to check if a string is numeric? If it doesn't, you can loop through the string and check if each character is a digit (using isDigit). If all characters in the string are digits, accept the input. Otherwise, reject it.

3) Take the result (if it passes step 1 and 2) and parse the last 2 characters (using substrings) into an integer, because those 2 characters will always be minutes. Take the remaining front character/s and parse it into another integer.

e.g. If I have variables "minutes" and "hours," a user-input of 2:00 will have its colon removed with replaceAll, resulting in 200. You take the last 2 characters of the string (substring(input length minus 2)) and parse that into an integer (minutes), and parse the remainder (substring(0, input length minus 3) into hours so minutes = 00 and hours = 2.

4) If minutes is >59 and hours is >24 and <0, reject the input, otherwise accept it and move on with your program.

I can't think of a more efficient way right now. It's 12:00am and I've been awake since 5:00.

Edit: Why the hell do I have to type a CAPTCHA to post? Something in my post was on Basil's godawfully sensitive filter and I had to end up typing a CAPTCHA 10 times trying to find what I had to remove.

Reply March 13, 2014 - edited
Arios

Can try getting an input of the form XX:YY, and parse the string using ":" as a delimiter. Then checking 0 <= XX <= 23 and 0 <= YY <= 59.

Reply March 13, 2014 - edited
MarshMallows

Nevermind code doesn't work

It should be a number, or else you can't restrict it too well.
I don't think it would work if you plan to use strings..

Reply March 13, 2014 - edited
bloodIsShed

entering the input as an integer, and making a lazy comparison like above suggests will not work
imagine if the user enters 2275, for example. 2275 < 2359, but 2275 should not be a valid input.

probably the easiest/lazy way is having the user enter the hour and the minute separately, like this
int hour, minute;
std::cout<< "enter hour: ";
std::cin >> hour;
if (hour < 0 || hour > 23)
//ask user to enter again

std::cout<< "enter minute: ";
std::cin >> minute;

if (minute < 0 || minute > 59)
//ask user to enter again

of course, you'll need to make a loop for each input

..or you could enter input as a string, and use string.substr(index, length) to get the number, convert the number using atoi or with a buffer,
and make sure the first number is between 0 and 23, and the second number is between 0 and 59, inclusive with an if comparison

Reply March 13, 2014 - edited
ehnogi

if

0 > && < 2359

else

'try again'

Reply March 13, 2014 - edited
LegendaryNee

Programming is tough, but I ask questions over at stackoverflow.com and usually they'll help you out. It's a great community.

Reply March 13, 2014 - edited