Hi guys, I have this program that doesn't work, can someone plz help me.
Write a program using command line arguments to perform arithmetic calculation, e.g. (1) to add: $ a.out 2.35 + 1.47 (2) to subtract: $ a.out 2.35 – 1.47
(3) to divide: $ a.out 2.35 / 1.47.
However to do multiplication you have to use
$ a.out 2.35 “*” 1.47 Notice the “ “ is used for the multiplication symbol otherwise, it will treated as a wildcat that matches everything.
2.35 and 1.47 are treated as string. To convert a string to floating point numbers, use atof(). You should print the result of the calculation from your program. In your program, (1) use a function to perform the calculation. In this function, use switch..case statement to make decision as to what operation to perform and return the result. (2) use a function to print the result. Make sure you provide checking for “invalid operation” and provide checking for “divide by zero”.
And here is the code that I wrote
Code:
#include<stdio.h>
float calcs(char,float arg[],float *ptr);
void print_result(int result);
float add(float argv[],float *ptr);
float sub(float argv[],float *ptr);
float mul(float argv[],float *ptr);
float div(float argv[],float *ptr);
int main(int argc, char *argv[])
{
float converted[argc];
float result;
float *ptr;
ptr=result;
char oper[2];
char ch;
for(ctr=1;ctr<argc;ctr++)
{
converted[ctr]=atof(argv[ctr]);
}
strcpy(oper,argv[2]);
ch=oper[0];
calcs(ch,converted[argc],*ptr);
print_result(result);
return 0;
}
float calcs(char ch, float argv[],float *ptr)
{
switch(ch)
{
case'+': add(argv,ptr);
break;
case'-': sub(argv,ptr);
break;
case'*': mul(argv,ptr);
break;
case'/': div(argv,ptr);
break;
default: printf("Invalid Operation\n");
break;
}
}
void print_result(float result)
{
printf("The result is:%f\n",result);
}
void add(float argv[],float *ptr)
{
ptr=argv[1]+argv[3];
}
void sub(float argv[], float *ptr)
{
ptr=argv[1]-argv[3];
}
void mul(float argv[], float *ptr)
{
ptr=argv[1]*argv[3];
}
void div(float argv[], flloat *ptr)
{
if(argv[3]!=0)
{
ptr=argv[1]/argv[3];
}
else
{
printf("Number should not be zero");
}
}
And here are the errors I am getting
Code:
assgn7b.c: In function `main':
assgn7b.c:16: incompatible types in assignment
assgn7b.c:20: `ctr' undeclared (first use in this function)
assgn7b.c:20: (Each undeclared identifier is reported only once
assgn7b.c:20: for each function it appears in.)
assgn7b.c:26: incompatible type for argument 2 of `calcs'
assgn7b.c:26: incompatible type for argument 3 of `calcs'
assgn7b.c: At top level:
assgn7b.c:49: conflicting types for `print_result'
assgn7b.c:5: previous declaration of `print_result'
assgn7b.c:54: conflicting types for `add'
assgn7b.c:6: previous declaration of `add'
assgn7b.c: In function `add':
assgn7b.c:55: incompatible types in assignment
assgn7b.c: At top level:
assgn7b.c:58: conflicting types for `sub'
assgn7b.c:7: previous declaration of `sub'
assgn7b.c: In function `sub':
assgn7b.c:59: incompatible types in assignment
assgn7b.c: At top level:
assgn7b.c:62: conflicting types for `mul'
assgn7b.c:8: previous declaration of `mul'
assgn7b.c: In function `mul':
assgn7b.c:63: incompatible types in assignment
assgn7b.c: At top level:
assgn7b.c:65: syntax error before "flloat"
assgn7b.c:66: conflicting types for `div'
assgn7b.c:9: previous declaration of `div'
assgn7b.c: In function `div':
assgn7b.c:67: `argv' undeclared (first use in this function)
assgn7b.c:69: `ptr' undeclared (first use in this function)
Thanks