Hi so i have this code and am really confused as to why it is not outputting invalid input, if i enter 4 / a into command argument, argc != 4 so therefore invalid input should be printed which is in the else statement? i also tried adding a separate statement but i could get that to work either. thanks
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
float numberOne;
float numberTwo;
float theResult;
char action;
sscanf(argv[1], "%f", &numberOne);
sscanf(argv[2], "%c", &action);
sscanf(argv[3], "%f", &numberTwo);
if (argc == 4) {
switch (action) {
case '+' :
printf("%f \n", numberOne + numberTwo);
break;
case '-' :
printf("%f \n", numberOne - numberTwo);
break;
case '*' :
printf("%f \n", numberOne * numberTwo);
break;
case '/' :
theResult = numberOne / numberTwo;
if (numberTwo == 0) {
printf("invalid output");
break;
} else if (theResult == -0) {
printf("invalid input");
break;
}
break;
}
} else {
printf("invalid input");
}
return(0);
}
When I run it:
$ ./a.out 4 / a
$
Why doesn't it print "Invalid input"?