954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Command line calculator

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
#include
#include
main(int argc, char *argv)
{
int answer;
if(argv[2]=='+')
answer=argv[1]+argv[3];
printf("%i",answer);
}

jifiii
Light Poster
45 posts since Nov 2004
Reputation Points: 10
Solved Threads: 1
 

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[])
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You