Computer Forums C - A calculator, need some help I guess.
 Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 05-27-2005, 07:44 PM #1 Daemon Poster     Join Date: Feb 2004 Posts: 523 C - A calculator, need some help I guess. Code: ```#include #include #include int main(int argc, char *argv[]) { int value, value2; int i; int sum; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'a': sscanf(argv[1], "%d", &value,&value2); sum = value + value2; printf("%d + %d = %d\n",value,value2,sum); break; default: fprintf(stderr,"Unknown argument: %s", argv[i]); break; } } } return 0; }``` Basicly I'm just playing around w/ arguments and whatnot, I need it to be like program -a 5 5 5 + 5 = 10 See anything wrong? __________________
 05-28-2005, 05:01 AM #2 Site Team     Join Date: Mar 2004 Posts: 7,872 Re: C - A calculator, need some help I guess. you need to decide if the syyntax for the function is -a or a and reflcet that in the switch statement... if you want case 'a': then the syntax is program a 5 5 if you want the syntax to be program -a 5 5 then the case statement is case '-a': a second problem is thatyou are scanning the niput to return numbers... what you should do is check argv[1] and argv[2] with the isnumeric function to check that thy are numbers and then use the atoi function to convert them to numbers... your fprintf statement, (whilst accurate) would be better being a printf statement... and the very last thing... you'd be wiser to make value 1 and value2 foats rather than ints. just to alow for decimal numbers, (otherwise decimals will give unexpected results.