0

OK, since i am not getting the answer am looking for, i am showing you a small source code of my program, i really hope that u understand it & sorry fot the newbie codez.

[
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
int numa = atoi(argv[1]);
int numb = atoi(argv[3]);
int sym = atoi(argv[2]);
int sum;
if (sym == 1)
{
sum = numa + numb;
printf("\n%d Plus %d= %d", numa, numb, sum);
}
else
printf("Error");
return 0;
}

/* INPUT: 1 1 1
OUTPUT: 1 Plus 1= 2 = GOOD :)

BUT

INPUT: 1.1 1 1.1
OUTPUT: 1 Plus 1= 2 =??????? HELP ME */
]

[]
2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Dave Sinkula
0

Integers are not floating point. Using sscanf to convert the command line text is my recommendation; with error checking, and using the + symbol itself, it might be as follows.

#include <stdio.h>
 #include <stdlib.h>
 
 int main(int argc, char **argv)
 {
    double a, b;
    if ( argc > 3 )
    {
 	  if ( sscanf(argv[1], "%lf", &a) == 1 &&
 		   sscanf(argv[3], "%lf", &b) == 1 )
 	  {
 		 switch ( argv[2][0] )
 		 {
 		 case '+':
 			printf("%g + %g = %g\n", a, b, a + b);
 			break;
 		 default:
 			puts("Error");
 			break;
 		 }
 	  }
    }
    return 0;
 }
 
 /* my input/output
 C:\Test>test 5.1 + 6.3
 5.1 + 6.3 = 11.4
 */
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.