Thread: I'm confused..

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    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!
    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;
    }
    
    }
    Last edited by .synq; 03-12-2011 at 02:18 PM.

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    71
    you write all functions in main?why?

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    61
    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.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    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!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with following code
    By SeriousTyro in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 10:37 AM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. Confused
    By (TNT) in forum C# Programming
    Replies: 1
    Last Post: 11-23-2005, 04:49 PM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM