Command line calculator

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 45
Reputation: jifiii is an unknown quantity at this point 
Solved Threads: 1
jifiii jifiii is offline Offline
Light Poster

Command line calculator

 
0
  #1
Nov 24th, 2004
I am trying to make a command line calculator, and I made up a partial code for it, just for adding numbers, I am jsut taking it one step at a time. But I am suppose a number then a space then the math function then a space then the second number. But for some reason I am always getting one for the output. Can anybody tell me what I am doing wrong with this.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main(int argc, char *argv)
{
int answer;
if(argv[2]=='+')
answer=argv[1]+argv[3];
printf("%i",answer);
}
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,460
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 252
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Command line calculator

 
0
  #2
Nov 25th, 2004
argv[1] and argv[3], if available, are strings. Convert them to integers before attempting to add them; atoi may be useful for this.

argv[2] is a string also. Use argv[2][0] for the first character.

[edit]D'oh! Didn't even look at main, which should be declared like this:
int main(int argc, char *argv[])
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC