-
I'm confused..
I'm trying to make a simple calculator. Everything's fine except my compiler is saying
||In function 'main':|
|49|error: redefinition of 'div'|
|28|note: previous definition of 'div' was here|
|86|error: redefinition of 'div'|
|49|note: previous definition of 'div' was here|
||=== Build finished: 2 errors, 0 warnings ===|
I originally had [code]
int div ( int x, int y);
int mult (int x, int y);
int add (int x, int y);
int sub (int x, int y);[code]
and I had div, mult, add and sub respectively in place of "div" in the code. Can anyone help me? I just wanna compile this! :D
Thank you
Code:
#include <stdio.h>
int div ( int x, int y );
int main()
{
int num;
printf( "What would you like to do?\n" );
scanf( "%d", &num );
if ( num == 1) {
int x;
int y;
printf( "Please enter two numbers that you would like to divide" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The division of these two numbers is %d\n", div( x, y) );
getchar();
}
int div ( int x, int y )
{
getchar();
return x / y;
}
if ( num == 2) {
int x;
int y;
printf( "Please enter two numbers that you would like to multiply" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The product of these two numbers is %d\n", div( x, y) );
getchar();
}
int div (int x, int y)
{
getchar();
return x * y;
}
if ( num == 3 ) {
int x;
int y;
printf( "Please enter two numbers that you would like to add" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The sum of these two numbers is %d\n", div( x, y) );
getchar();
}
int dib (int x, int y)
{
getchar();
return x + y;
}
if ( num == 4 ) {
int x;
int y;
printf( "Please enter two numbers that you would like to subtract" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The difference of these two numbers is %d\n", div( x, y) );
getchar();
}
int div (int x, int y)
{
return x - y;
}
}
-
you write all functions in main?why?
-
Try this:
Code:
#include <stdio.h>
int div( int x, int y );
int mult(int x, int y);
int subt(int x, int y);
int add(int x, int y);
int main()
{
int num;
printf( "What would you like to do?\n" );
scanf( "%d", &num );
if ( num == 1)
{
int x;
int y;
printf( "Please enter two numbers that you would like to divide" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The division of these two numbers is %d\n", div( x, y) );
getchar();
}
if ( num == 2)
{
int x;
int y;
printf( "Please enter two numbers that you would like to multiply" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The product of these two numbers is %d\n", mult( x, y) );
getchar();
}
if ( num == 3 )
{
int x;
int y;
printf( "Please enter two numbers that you would like to add" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The sum of these two numbers is %d\n", add( x, y) );
getchar();
}
if ( num == 4 )
{
int x;
int y;
printf( "Please enter two numbers that you would like to subtract" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The difference of these two numbers is %d\n", subt( x, y) );
getchar();
}
return 0;
}
int div( int x, int y )
{
getchar();
return x / y;
}
int mult(int x, int y)
{
getchar();
return x * y;
}
int subt(int x, int y)
{
getchar();
return x - y;
}
int add(int x, int y)
{
getchar();
return x + y;
}
Always, declare all your functions without forgetting any function before the main function, and always define your functions outside the main function.
study the code to understand more ;)
God Luck.
-
Sorted it out with
Code:
#include <stdio.h>
int div ( int x, int y );
int mult ( int a, int b );
int add ( int c, int d );
int sub ( int e, int f );
int main()
{
int num;
printf( "What would you like to do?\n\n1. Divide two numbers\n2. Multiply two numbers\n3. Add two numbers\n4. Subtract two numers\n" );
scanf( "%d", &num );
if ( num == 1) {
int x;
int y;
printf( "Please enter two numbers that you would like to divide" );
scanf( "%d", &x );
scanf( "%d", &y );
printf( "The division of these two numbers is %d\n", div ( x, y) );
getchar();
}
if ( num == 2) {
int a;
int b;
printf( "Please enter two numbers that you would like to multiply" );
scanf( "%d", &a );
scanf( "%d", &b );
printf( "The product of these two numbers is %d\n", mult ( a, b) );
getchar();
}
if ( num == 3 ) {
int c;
int d;
printf( "Please enter two numbers that you would like to add" );
scanf( "%d", &c );
scanf( "%d", &d );
printf( "The sum of these two numbers is %d\n", add ( c, d) );
getchar();
}
if ( num == 4 ) {
int e;
int f;
printf( "Please enter two numbers that you would like to subtract" );
scanf( "%d", &e );
scanf( "%d", &f );
printf( "The difference of these two numbers is %d\n", sub ( e, f ) );
getchar();
}
return 0;
}
int div ( int x, int y )
{
getchar();
return x / y;
}
int mult ( int a, int b )
{
getchar();
return a * b;
}
int add ( int c, int d )
{
getchar();
return c + d;
}
int sub (int e, int f )
{
getchar();
return e - f;
}
Thanks guys!