Computing.Net > Forums > Programming > Help w/ C Programming CmdLine Args

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help w/ C Programming CmdLine Args

Reply to Message Icon

Name: Fluffyinsanity
Date: January 9, 2005 at 02:25:30 Pacific
OS: XP
CPU/Ram: 1gb
Comment:

I'm trying to make a simple program in C that takes these arguments: math.exe (first number) (math operator, like +, -, etc)(second number). The program compiles fine but, doesn't work at all. Here's my source code:

#include (stdio.h)
#include (stdlib.h)
int main(int argc, char *argv[])
{
float num1, num2, answer;
while(argc < 3)
{
printf("\n\nUSAGE: add <first number> <operator> <second number>\n\n");
exit(1);
}
num1 = atof(argv[1]);
num2 = atof(argv[2]);
if(argv[3] == "+")
{
answer = num1 + num2;
printf("%f + %f = %f",num1,num2,answer);
}
else if(argv[2] == "-")
{
answer = num1 - num2;
printf("%f - %f = %f",num1,num2,answer);
}
else if(argv[2] == "/")
{
answer = num1 / num2;
printf("%f / %f = %f",num1,num2,answer);
}
return 0;
}



Sponsored Link
Ads by Google

Response Number 1
Name: bamakhrama
Date: January 9, 2005 at 04:34:11 Pacific
Reply:

Hi,
you need to use if(strcmp(argv[3],"+")==0) instead of == operator.
Here is the working program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
float num1, num2, answer;
while(argc < 3)
{
printf("\n\nUSAGE: add <first number> <second number> <operator>\n\n");
exit(1);
}
num1 = atof(argv[1]);
num2 = atof(argv[2]);
if(strcmp(argv[3],"+")==0)
{
answer = num1 + num2;
printf("%f + %f = %f",num1,num2,answer);
}
else if(strcmp(argv[3],"-")==0)
{
answer = num1 - num2;
printf("%f - %f = %f",num1,num2,answer);
}
else if(strcmp(argv[3],"/")==0)
{
answer = num1 / num2;
printf("%f / %f = %f",num1,num2,answer);
}
else
{
printf("\n\nUSAGE: add <first number> <second number> <operator>\n\n");
exit(1);
}
return 0;
}

--
mohameda@ieee.org


0

Response Number 2
Name: Fluffyinsanity
Date: January 9, 2005 at 14:16:44 Pacific
Reply:

Oh, I see now! I forgot that the elements stored in argv were strings. Thank you very much for the help!


0

Response Number 3
Name: BlueRaja
Date: January 9, 2005 at 22:14:24 Pacific
Reply:

printf("\n\nUSAGE: add <first number> <operator> <second number>\n\n");
num2 = atof(argv[2]);
if(strcmp(argv[3],"+")==0)

Hmm....


BlueRaja.admin@gmail.com


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help w/ C Programming CmdLine Args

Need help with C programming www.computing.net/answers/programming/need-help-with-c-programming/6970.html

need help w/ java programming www.computing.net/answers/programming/need-help-w-java-programming/9632.html

Need help with C++ program www.computing.net/answers/programming/need-help-with-c-program/5152.html