How would I detected an invalid argument like these.
+212+21-2
12+3
+3-1
ect
I tried counting the amount of operators but it didnt work, I also couldnt find anything online, so what would be the best approach. I already tried using a double for loop to count the amount of each operator but it didnt work.
for (int i = 1; i < 2; i++)
{
int x = 0;
for (int j = 0; j < strlen(argv[i]); j++)
{
int L = 0;
if (isdigit(argv[i][j]) || argv[i][j] == '.' || argv[i][j] == '-' || argv[i][j] == '+' || argv[i][j] == 'e' || argv[i][j] == ',' || argv[i][j] == 'E')
{
w = w + 1;
cout << argv[i][j];
// count the amount of operators
int x2 = 0;
//Checks '.'
if (argv[i][j] == '.')
{
x = x + 1;
j = j + 1;
if (x > 1) // if there is more than one '.'
{
cout << "X" << endl;
return 0;
}
if (argv[i][j] == 'e') // checks that there isnt an e after the '.' e.g 1.e+2
{
cout << "X" << endl;
return 0;
}
else
{
j = j - 1;
}
}
if (argv[i][j] == 'e' || argv[i][j] == 'E')
{
L = L + 1; // checks for multiple e's (ee)
j = j + 1; // checks for invalid e's (e-1)
if (L > 1)
{
cout << "X" << endl;
return 0;
}
if (argv[i][j] == '-')
{
j = j + 1;
if (isdigit(argv[i][j])) // checks that the power is valid, this is nonvalid 1.2e+-2
{
j = j - 1;
}
else
{
cout << "X" << endl;
return 0;
}
}
if (argv[i][j] == '+')
{
j = j + 1;
if (isdigit(argv[i][j])) //checks for double powers
{
j = j - 2;
}
else
{
cout << "X" << endl;
return 0;
}
}
else
{
j = j - 1;
}
}
if (argv[i][j] == '-') // | this is the beginning
{
SUBCOUNT = SUBCOUNT + 1;
if (SUBCOUNT > 1 && ADDCOUNT >= 1)
{
cout << "x" << endl;
return 0;
}
j = j + 1;
if (argv[i][j] == '-')
{
cout << "x" << endl;
return 0;
}
elseif (argv[i][j] == '+')
{
cout << "x" << endl;
return 0;
}
else j = j - 1;
if (argv[i][j] == '-')
{
cout << "x" << endl;
return 0;
}
elseif (argv[i][j] == '+')
{
cout << "x" << endl;
return 0;
}
else j = j - 1;
}
if (argv[i][j] == '+')
{
ADDCOUNT = ADDCOUNT + 1;
if (ADDCOUNT > 1 && SUBCOUNT >= 1)
{
cout << "x" << endl;
return 0;
}
j = j + 1;
if (argv[i][j] == '-')
{
cout << "x" << endl;
return 0;
}
elseif (argv[i][j] == '+')
{
cout << "x" << endl;
return 0;
}
else j = j-1;
return 0;
if (SUBCOUNT > 1 && ADDCOUNT >= 1)
{
cout << "x" << endl;
}
j = j + 1;
if (argv[i][j] == '-')
{
cout << "x" << endl;
return 0;
}
elseif (argv[i][j] == '+')
{
cout << "x" << endl;
return 0;
}
else j = j - 1;
} // | this is the end
if (argv[i][j] == ',')
{
string str = argv[1];
for (int i = 0, len = str.size(); i < len; i++)
{
// check whether parsing character is punctuation or not
if (str[i] == ',')
{
str.erase(i--, 1);
len = str.size();
argval1 = atof(str.c_str());
V2 = 1;
}
}
}
if (w == strlen(argv[i]))
{
//send command to OUSB board
pinc = ReadPinC();
if (pinc > 255)
{
cout << "Y" << endl;
return 0;
}
if (pinc < 0)
{
cout << "Y" << endl;
return 0;
}
if (V2 != 1)
{
argval1 = atof(argv[1]);
}
}
}
else
{
cout << "X" << endl;
return 0;
}
}
}
here is the snippet of my code i cant fit all of it in aa it goes over the character limit, I have 3 inputs that I need to compare and this code checks that the input via command arguments is 1 number( 1e+10, 1,000, 1.01, 0.001 ect) not +212+21-2, I used a counter but it isnt working. this is because I dont know how to test the infinite number of options there are.
It calls the function getVal() to check each input string.
For that string to be valid it has to contain one number exactly.
The string is put into a stringstream, to isolate it from any other input and make sure that input failures on that stream do not leave other streams in a failed state. That stringstream acts like any other input stream (i.e. you can use the >> operator).
ss >> result will try to read a double from that stream (possibly with white space in front, which is allowed). It then tests the state of the stream if ( !( ss >> result ) ) returnfalse;
to check whether that read was successful.
The second test if ( ss >> remainder ) returnfalse;
makes sure there is nothing else (other than white space) left on the line.
bool getVal( string input, double &result )
{
string remainder;
stringstream ss( input );
if ( !( ss >> result ) ) returnfalse; // Invalid (first item not a number)
if ( ss >> remainder ) returnfalse; // Invalid (something else on the line)
returntrue;
}
This is a very inefficient way of performing std:string to double conversion - although it's simple code. If performance is an issue (probably isn't here), then using std::from_chars() is much more efficient than >> (or std::strtod() if your compiler doesn't yet support std::from_chars() for double/float).