I'm quite new to C, and just started on using functions, but I'm quite sure this code is right, but my compiler displays an error within the functions.
#include <stdio.h>
int option,var1,var2,exec;
int add(int add1,add2);
int sub(int sub1,sub2);
int multi(int multi1,multi2);
int divid(int divid1,divid2);
main()
{
printf("Please enter a value: ");
scanf("%d",&var1);
printf("Please choose an operation:");
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division\n");
scanf("d",&option);
printf("Please enter a second value: ");
scanf("d",&var2);
if (option == 1)
exec=add(var1,var2);
else
if (option == 2)
exec=sub(var1,var2);
else
if (option == 3)
exec=multi(var1,var2);
else
exec=divid(var1,var2);
printf("\nThe Solution is %d.\n",exec);
return 0;
}
int add(int add1,add2)
{
int x_add;
x_add=add1+add2;
return x_add;
}
int sub(int sub1,sub2)
{
int x_sub;
x_sub=sub1-sub2;
return x_sub;
}
int multi(int multi1,multi2)
{
int x_multi;
x_multi=multi1*multi2;
return x_multi;
}
int divid(int divid1,divid2)
{
int x_divid;
x_divid=divid1/divid2;
return x_divid;
}
jamis
07-15-2001, 07:57 PM
What exactly does the compiler complain about? Whats the error?
qball
07-15-2001, 08:19 PM
if (option == 1)
exec=add(var1,var2);
else
if (option == 2)
exec=sub(var1,var2);
else
if (option == 3)
exec=multi(var1,var2);
else
exec=divid(var1,var2);
Should be:
if (option == 1)
{exec=add(var1,var2);}
else
if (option == 2)
{exec=sub(var1,var2);}
else
if (option == 3)
{exec=multi(var1,var2);}
else
{exec=divid(var1,var2);}
Or sumtin like that, you need to mind your braces.
[This message has been edited by qball (edited 07-15-2001).]
zskillz
07-15-2001, 11:49 PM
I don't know C well enough to be positive about this, but I thought that if there are no brackets, then the if statement performs the command directly after it, but then continues on with the rest of the code.
-Z
emcron
07-16-2001, 05:58 AM
If you post the error message you get, it would help a lot. First I dont think the braces are the problem. Also if you want to make you program simpler use the "switch" statement.
jamis
07-16-2001, 07:32 AM
Actually.. i just noticed something...
These are your prototypes:
int add(int add1,add2);
int sub(int sub1,sub2);
int multi(int multi1,multi2);
int divid(int divid1,divid2);
Don't make the parameters into lists like you declare your variables (btw - global variables should be avoided if you can.. infact, by using globals like you have, you don't even need to pass the arguments.. see my adjustment below).. try changing the first few lines to this:
int add(int add1, int add2);
int sub(int sub1, int sub2);
int multi(int multi1, int multi2);
int divid(int divid1, int divid2);
main()
{
int option,var1,var2,exec;
Also do it on the functions themselves... add the argument types to each variable.
qball
07-16-2001, 11:39 AM
zskillz,
Correct for simple if statement (inline). Once if-then-else is desired, needum brackets.
Curious as to the actual problem.
Wilan Wong
07-16-2001, 04:42 PM
Thanks Jamis, I'll try your adjustment when I get back from school! I'll see if the adjustment works and I'll post it up.
techwriter
07-18-2001, 10:14 AM
Hi Wilan,
Here's your debugged code. Your errors were in your prototype statements like Jamis pointed out. You need to specify the data type for each parameter. In other words, you can't do int function (int x,y,z);
Then after you fix the prototypes, you'll have to copy and paste them to replace the function definitions (just don't copy the ";").
Hope this helps. Also, you should get in the habit of commenting your code more.
Best regards,
techwriter
#include <stdio.h>
int option,var1,var2,exec;
int add(int add1,int add2);
int sub(int sub1, int sub2);
int multi(int multi1, int multi2);
int divid(int divid1, int divid2);
main()
{
printf("Please enter a value: ");
scanf("%d",&var1);
printf("Please choose an operation:");
printf("\n1. Addition");
printf("\n2. Subtraction");
printf("\n3. Multiplication");
printf("\n4. Division\n");
scanf("%d",&option);
printf("Please enter a second value: ");
scanf("%d",&var2);
if (option == 1)
exec=add(var1,var2);
else
if (option == 2)
exec=sub(var1,var2);
else
if (option == 3)
exec=multi(var1,var2);
else
exec=divid(var1,var2);
printf("\nThe Solution is %d.\n",exec);
return 0;
}
int add(int add1,int add2)
{
int x_add;
x_add=add1+add2;
return x_add;
}
int sub(int sub1, int sub2)
{
int x_sub;
x_sub=sub1-sub2;
return x_sub;
}
int multi(int multi1, int multi2)
{
int x_multi;
x_multi=multi1*multi2;
return x_multi;
}
int divid(int divid1, int divid2)
{
int x_divid;
x_divid=divid1/divid2;
return x_divid;
}
[This message has been edited by techwriter (edited 07-18-2001).]
Wilan Wong
07-21-2001, 05:34 AM
Thanks everyone for your replies.. the source compiled successfully! But, the program ended after typing out the first value, and it just printed out the options available.. and the program ended.. wot did i do wrong this time!? I'm very new to C, and the language is so different from the ones i previously done, which was vb.
techwriter
07-24-2001, 01:51 AM
Wilan,
I'm not sure I understand your question here: "...the source compiled successfully! But, the program ended after typing out the first value, and it just printed out the options available.. and the program ended..."
You should just copy and paste the code I gave you in my previous reply into your compiler, then run it. It worked on my machine using MS visual C++ 6.0 compiler.
If you're asking why the program stops after computing the first value, its because you programmed it to do so. If you want the program to continue printing the menu after computing the first two values, you should put your menu in a do...while loop and also, add an option to quit the program. The do portion basically saying to display the menu, and the while condition being while not the value you decide for stopping the program.
Hope this helps clear things up a bit.
SysOpt.com
Copyright WebMediaBrands Inc. All Rights Reserved.