C - A calculator, need some help I guess.

DevilHack

Daemon Poster
Messages
523
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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?
 
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.
 
Back
Top Bottom