Hello WoodyW00,
I believe the root of your problem is a misunderstanding.
Whether it is "argv[1]"or "argv[2]" your brain sees "12.34.56" as an invalid number, but the "atof" or "srttof" functions do not.
Looking closer at the number you see "12 point 34 point 56" and the functions see "12 point 34
period 56".
Both "atof" and "srttof" will convert the string into a float until it finds a non digit character or new line whichever comes first. This makes "12.34" a valid number and the rest is ignored.
You could check the string for more than 1 (.) character and consider that to be an invalid number or take what can be converted.
Working with
salem c's example I came up with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include<iostream>
int main(int argc, char* argv[])
{
float num1{};
char* str{ nullptr };
std::cout << "\n " << argv[1] << '\n';
num1 = strtof(argv[1], &str);
std::cout << "\n " << num1 << '\n';
return 0;
}
|
Which produces:
In your if statement
if ((z == 'a') || (z == 's') || (z == 'm') || (z == 'd') || (z == 'p') || (z == 0))
In the last or I am not sure what you are checking. I s it the number (0), a "\0" or the character '0'? It ma not make any difference, but it could. Also since it does not match the rest of the if statement it tends to make one stop and ponder what you are meaning and if it will work.
I have not tested your code yet, so I do not know how it actually works. Just a thought for now.
A last note "ctime", "cmath" and cerrno" are the better choice for these header files.
Andy