function error

This is a discussion on function error within the C Programming forums, part of the General Programming Boards category; /* Hai, I am having trouble in simple function, when compile there is an error "type mismatch in redeclaration of ...

  1. #1
    void99
    Guest

    function error

    /*
    Hai,
    I am having trouble in simple function, when compile there is an error
    "type mismatch in redeclaration of count" I don't know what is wrong with
    my code. For me it looks fine nothing wrong.
    What is wrong with my code ? (I am using Borland 3.1 DOS and win98.)

    Thanks for any help!

    */

    #include<stdio.h>
    #include<conio.h>



    void main()
    {
    float salary,bonus,sale;
    clrscr();
    total=0;

    printf("Enter Your Salary : ");
    scanf("%f",&salary);

    printf("Enter Your Sale : ");
    scanf("%f",&sale);

    printf("Total Salary : %f",count(sale,salary) );

    getch();
    }



    float count(float sale, float salary)
    {
    float bonus,total;

    if (sale>=1000000)
    bonus = sale * 0.5;
    else
    if ( sale >=500000)
    bonus = sale * 0.3;
    else
    bonus = sale * 0.1;

    total = salary + bonus;

    return total;
    }

  2. #2
    Unregistered
    Guest
    Ok - First, make sure your main() function returns a value.

    Secondly you did not prototype the function count(), which was causing your error I think. If you are going to define a function after you call it in your source code you have to prototype the function to ensure the compiler knows what to expect.

    You also didn't declare the int total before you used it.

    Code:
    #include<stdio.h>
    
    // Prototype of function count
    float count(float , float );
    
    int main(void)
    {
      float salary,bonus,sale;
    
      // Didn't declare this
      int total=0;
    
      printf("Enter Your Salary : ");
      scanf("%f",&salary);
      printf("Enter Your Sale : ");
      scanf("%f",&sale);
      printf("Total Salary : %f",count(sale,salary) );
    
      return 0;
    }
    
    float count(float sale, float salary)
    {
      float bonus,total;
    
      if (sale>=1000000)
        bonus = sale * 0.5;
      else if ( sale >=500000)
        bonus = sale * 0.3;
      else
        bonus = sale * 0.1;
      total = salary + bonus;
    
      return total;
    }

  3. #3
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343

    Oops

    Ok - that was me before - Caught out by the posting at a different computer, lack of cookies trick..

    What I meant before was, make sure main() returns a value of type int. Very important or else your program will become undefined.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    9

    Talking funtion error reply

    your function ,

    float count(float sale ,float salary);

    modify this and you will get the correct result,

    double count(double sale,double salary);

    make return value as double

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21