if you use the atof() function, which is used to convert a string to a floating‐point number (type double), the only valid argument to pass into the atof() function in the command line arguments phase ‘Hello World 123.54’ is ‘123.54’, as this could be the number one hundred and twenty three point fifty‐four. The other arguments ‘Hello’ and ‘World
’ would result in 0, as the characters in each of those arguments does not represent a valid numerical number.
Your task is to extend your program to determine if the characters in each argument are suitable for
conversion to a double (which is a larger size float). Your program should handle a minimum of 3
arguments and display the text “invalid input” if the argument contains characters other than an
alphanumerical characters and one '.' decimal point. If the argument contains valid alphanumerical
characters and one decimal point, then it should be converted to a double and displayed to the console
screen. Each input argument should be processed individually and the output should be on a separate
line in the console. For example if the command line arguments phrase ‘Hello World 123.54’ was used,
the console output would be:
invalid input
invalid input
123.54
I tried very hard but I can't really figure out anything.
Could anyone please suggest codes for this problem? I tried using atof(), nested for loop, if, string. but I can't seem to make it working.
Here is a program that will solve the problem. I have left out the code that actually determines whether the argument appears to be a double (the body of function isDouble()).
#include <iostream>
// examine the argument and return true if it's a valid number, false
// otherwise
bool isDouble(constchar *arg)
{
// INSERT YOUR CODE HERE
}
int
main(int argc, char **argv)
{
// Loop through the arguments. Note that argv[0] is the name of the program
// so the "real" arguments start at argv[1]
for (int i=1; i<argc; ++i) {
if (isDouble(argv[i])) {
double d = atof(argv[i]);
std::cout << d << '\n';
} else {
std::cout << "invalid input\n";
}
}
}
It would be better if you divide this task in 2 parts.
One parts checks if one argument is correct and the second part loops through all the arguments.
Like so:
bool validDouble(constchar *arg)
{
// your code here
}
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; i++)
{
if (validDouble(argv[i]))
{
// convert argv[i] to double and display it
}
else
{
// output error msg
}
}
system("pause");
return 0;
}
BTW. I recommend to do a google search for "divide and conquer" Dividing a problem into smaller sub-problems makes things much easier.
what do you get if the user is a jerk and types hello 0 world 0.0 ? :)
I think youll want to check to see if the argument actually IS zero.
You *may* want to handle alternative local format of , instead of . (??) some countries reverse the meaning of , and . so 123.45 is 123,45 and 1,234 is 1.234. The default is the american way.
If the input string contains a number and nothing else, after the function call the pointer pEnd should point to the terminating null character. You might use that to test for valid result.
There is a (small) bug in the if(character ... etc logic which you'll need to fix because if the last character is true then all previous checks which might be false are overridden :)
> determine if the characters in each argument are suitable for conversion to a double
This is hard, unless we use the standard library; there are too many edge cases, and to make matters worse, the specific characters that can represent a floating point value is locale-dependent.