Go Back   Computer Forums > General Computing > Programming
Join Computer forums Today

Thread Tools Search this Thread Display Modes
Old 05-27-2005, 07:44 PM   #1
Daemon Poster
DevilHack's Avatar
Join Date: Feb 2004
Posts: 523
Send a message via AIM to DevilHack
Default C - A calculator, need some help I guess.

#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);

  fprintf(stderr,"Unknown argument: %s", argv[i]);
 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?

DevilHack is offline   Reply With Quote
Old 05-28-2005, 05:01 AM   #2
Site Team
root's Avatar
Join Date: Mar 2004
Posts: 7,872
Default 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.
root is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

All times are GMT -5. The time now is 12:21 AM.

Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2016, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0