I have a program which intakes an hour input and minute input,
I want to figure out how to limit the hour input to one digit, and limit the minutes input to two digits.
As well, for the minutes input I would like to be able to detect when there is only one input character so I can add a "0" to the time output.
So it wouldn't output 9:1, rather than 9:01
I want to incorporate the current while statement I have that eliminates any possible non-numeric input.
okay I obviously used a bad example because all problems are more logically overcome with < and >, though I am literally wanting to know how to determine if an input has a certain number of characters in it, and how to limit the number of input characters.
a way that would be able to work with alphabetical characters as well.
thankyou that helps. Now I can capture the number of characters and limit possibilities to certain numbers of characters.
However, I still need to know how to input a character in the beginning of a string.
For example, if 1 is entered for minuted, I would be able to identify if a string size = 1, the variable would require a "0" inserted to the beginning
In another example, if I identify that someone enters say 3 characters, I would want to add either a 0 or whatever to the beginning of that string.
~ for whatever reason.
I seem to be getting this error I try an if statement where (minutes = "60")
minutes being a string
error C2451: conditional expression of type 'std::basic_string<_Elem,_Traits,_Ax>' is illegal
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
its everytime i set a string equal to some characters in an if statement, how do I get around this?
Not sure without seeing your code, but make sure you are doing the following:
if(strcmp(minutes,"60"))
and not
if(minutes = "60") WRONG
The first is a logical comparison, the second is an assignment. Also, it's good practice if you are going to use a constant value like 60 for minutes to create a const variable or global definition and set it, that way if you need to make adjustments to this value later, you only have to change it in one place. Good luck and please post your full code if you continue to have issues.
This is wrong, regardless of how minutes is defined. In order to get this to compile, the type of minutes has to be (something implicitly convertible to) const char * (looking at the original code makes me think this is unlikely). But even if minutes has an appropriate type, this is not how strcmp works.
If you omit lines 50-52 and change line 53 to if (minss ==60) and line 57 to minss = 0; it completely works except when "60" is entered for the minutes input, the output turns out to be nothing...
lines 62-64 i added to convert the int type "minss" to string "minutes"
@m4ster r0shi I may be missing how to fully implement the code you provided with mine and still adding the wrong input check, sorry.
Here is my revised code
I identified string minutes outside the while statement for the input
and set the end output to display the string "minutes"
all only because it requires the double 0 "00" safety if "60" is the input (as hours is + 1)
though the only problem now, is that of course since the minutes is a string, if zeroes are added before the actual number when being inputted, the zeroes will also be outputted.
unlike hours, which is always an integer, it can not display zeroes before a number on the output.
I tried adding to the end stoi but for some reason it's giving me an error saying stoi is unidentified..?
though the only problem now, is that of course since the minutes is a string, if zeroes are
added before the actual number when being inputted, the zeroes will also be outputted
Add a check like this -> if (minutes.size() > 2) { /* ... */ }
next to your existing -> if (minutes.size() < 2) { /* ... */ }.
Inside the body of the new if statement, set minutes to be
the substring of itself that contains its last two characters.
Another problem you have now, though, is that minutes might also contain trailing spaces. Perhaps a better solution would be to just convert minss back to a string once you verify that you have a valid input:
interestingly, I placed || (minutes.size() > 2) right on line 34 and now the one and only error is that the if (minutes.size() < 2) on line 58 now gets overlooked if there is a space before the a single digit
So then a zero won't be added, it will be 12: 1
I dont know how to retrieve the first value of a string and check if it's equal to a space.
maybe convert to char and cout.put() and ascii a space and check that but I'm even having problems converting a string to char...
If you convert minss back to a string as I do in my edit above, you won't have this problem, as any trailing and leading spaces will disappear. Actually, you don't even have to add the extra condition you mention here. But if you want to do it your way, you can access individual characters of a std::string using the [] operator (i.e. the same way you would access elements of an array).