Ok well i know that isdigit in my code is seeing the negatives as a character and so i made my program give an error message. But i need negative numbers to work how could i get this to work with negative numbers.
The code is supposed to add the 3 numbers input into the command line argument including negative numbers.
The code is supposed to add the 3 numbers input into the command line argument including negative numbers.
Code:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> int main(int argc, char *argv[]){ int add=0; int i; int sum; if(argc == 4){ for(i=1; i<argc; i++){ if(isdigit(*argv[i]) == 0){ fprintf(stderr, "%s: found non-numerical arg\n",argv[0]); return EXIT_FAILURE; } else if(isdigit(*argv[i]) !=0){ sscanf(argv[i],"%d",&sum); add = add +sum; } } printf("%d\n",add); } else{ fprintf(stderr,"incorrect number of args\n"); } return EXIT_SUCCESS; }
Comment