Validation of input Procedural C++ [duplicate] - c++

This question already has answers here:
How do I iterate over the words of a string?
(84 answers)
Closed 8 years ago.
I need some tips on an assignment that I am currently working on. I need to create a program for converting time specs (n1y n2n n3d n4h n5m n6s -- note the amount of whitespace can vary between the different inputs) to seconds. One of the functions that I need to create is a function to validate the time spec input (as a const char*). The function header looks as follows:
// is_legal_time_spec:
// Checks if ctime_spec is a valid time spec
// In:
// ctime_spec != NULL
// Out:
// return -- true if the ctime_spec is a valid time spec
// false if otherwise
bool is_legal_time_spec( const char *ctime_spec ) {
return false;
}
Ive had some ideas about how to use the strpbrk(3) function for this, but I was wondering if there is another easier function that I can use to check the format of the time spec input.
Thanks in advance for your help.

Use strtok to break string into words, and then validate each words separately by parsing it or using sscanf. Then do simple mathematics to convert into time specs into seconds

Related

c++, converting input string to number, failure to convert should not return zero [duplicate]

This question already has answers here:
Is there a TryParse equivalent in C++ (gcc)?
(4 answers)
Closed 7 years ago.
Hi im looking at the string conversion methods in c++ strtol, strtoll, strtoul, atol etc.
It seems every single one of them will return a 0 (or 0.0) as a result for a string that was impossible to convert.
Assuming that 0 is a completely valid correct input for my program how do I avoid converting nonsense strings to zero and make for example them throw some exception instead? Are there ready made functions that perform that way or do I have to wrap these with some of my own code.
Also could anybody explain me what might the reasons be that these functions are designed like this (Why build in the trouble of separating nonsense string parse result from hones-to-god "0" string parse results)?
Failure is indicated by setting errno, which is the old-fashioned C way of doing things.
You might find stol and friends more useful - they indicate failure by throwing an exception.
http://en.cppreference.com/w/cpp/string/basic_string/stol
You can use the std::stoi() std::stof() function family to do this. The functions will throw an exception for invalid input.
You may find useful boost/lexical_cast.

How to validate an integer properly in C++? [duplicate]

This question already has answers here:
How to determine if a string is a number with C++?
(36 answers)
How to convert a command-line argument to int?
(8 answers)
Closed 9 years ago.
I want to validate the input by user to make sure it's an integer (fully). I've tried a few methods, like !cin, but none of them work properly..
Most of methods fail to validate input like this:
32tgf
When there is a number first and then letters, it doesn't fail, but it takes it as valid entry..
Note: It's a project for college and it's specified that the variable should be of type int.
Read into a string, then use e.g. std::stoi or std::strtol to both convert to an integer and validate the input.
Or read into a string, put that string in a std::istringstream which you use to extract the integer. Then check if there's anything more in the istringstream.
I'd recommend the first method though.

Double.parseDouble() equivalent in C++? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
std::string to float or double
I am writing a calculator (learning C++), and just decided to make a calculator, since that was the first thing I did when learning Java.
The program does the following:
Asks the user for the first number
Asks what the user wants to do with the number (-,+,*,/)
Asks for the second number
Displays the result.
when grabbing a number from the user in Java I used Double.parseDouble(number) to check if what they entered is a number or not.
Is there a similar command in C++? Ive been doing research and it seems like you have to use tricks such as comparing it to ASCII equivalents etc.. basically a ton of code for a simple task... so before i take that route, I wanted to stop by here and see if perhaps there is some sort of call I can make to check if the input is a number. I need it to validate negatives, zero and positives, as well as numbers with decimals... everything else should be rejected and the user should be asked for input again.
When I did it in Java I used try/catch statement and if the input was invalid it would return the method (in other words, itself) so it would loop and ask the user for input again.
Thanks!
You can use strtod. It handles underflow and out of range values in a convenient way.
Additionally, as Joachim Pileborg notes, if you use C++11 compliant compiler, there is std::stod in the standard library.
Use the function double atof(const char*) ;
example usage:
const char* = "3.14159";
double pi = atof(myDouble);
How about using isdigit function.

C++: Comparing two strings [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
comparing two strings with comma seperated values
I am working in C++, where I have two strings:
string str1 = "1,4,8,",
str2 = "4,1,8,";
Both strings contains comma separated values. Now I just want to check whether all the elements in str1 also exist in str2, regardless of their position. Is there any direct way to check this? Do I need to write custom code for this?
As far as C++ is concerned, those strings are just sequences of characters. If you apply meaning to those characters (such as "comma separated values"), then you'll have to write some code to extract the data and deal with it.
I would do something like:
split the string on ','
convert each sequence of digits into an integer (skipping over empty elements)
insert those integers into a set (one for each input string)
compare the sets
It's up to you to determine what kind of integer to use.
Yes, you need to write custom code, although not a lot of it. Once you figure out the algorithm you can post here if you have further questions on how to implement each part.

Comparing chars together? [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 6 years ago.
I'm fairly new to C++ and have run into a problem when trying to compare two chars, here is an example:
#define PartOne "He"
#define PartTwo "llo"
char Final1Var[] = PartOne PartTwo;
char ComapreVars[] = "Hello";
if(Final1Var == ComapreVars)//This is were the problem occurs, the chars are supposed to be equal to each other BUT for some reason the 'if' statement ends up determining they're not?
InGameDialog::Alert("They Match");
else
InGameDialog::Alert("They Don't Match");
What is going wrong with the code? I can't imagine why this wouldn't work? Any suggestions?
in c++ character array's could not be compared using == operator you have to use strcmp function or string comparison function.
This is a very common question. I'll mark it as duplicate once I find a good answer to this among the other questions.
In the mean time, you're not comparing chars, you're comparing char[]s, which is entirely different. You'll want to use strcmp, strncmp, or std::string types would be an even better solution.
What is array decaying? has some reasonable explanations for what's going on in your code and why.