Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Help with C error 'conflicting types'

  1. #1
    Join Date
    Jan 2010
    Beans
    137
    Distro
    Ubuntu 10.04 Lucid Lynx

    Help with C error 'conflicting types'

    I am trying to round money to the nearest cent. When I change calcStartBalance to return type int it works, but not with double. I keep getting the error 'conflicting types for function calcStartBalance'. help?

    This is the problem piece of the code I have:

    Code:
    #define HUNDRED 100.00
    #define POINT005 0.005
    
    double roundToHundreths(double a)
    {
      double b = 0.0;
      b = a + POINT005;
      b *= HUNDRED;
      b = (int)b;
      b /= HUNDRED;
      return b;
    }
    
    double calcStartBalance(double start_b, double down_p)
    {
      return (roundToHundreths(start_b - down_p));
    }
    Let books be your dining table, and you shall be full of delights,
    Let them be your mattress, and you shall sleep restful nights.

  2. #2
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Help with C error 'conflicting types'

    The code you posted works fine on my system; this is what I used:
    Code:
    #include <stdio.h>
    
    #define HUNDRED 100.00
    #define POINT005 0.005
    
    double roundToHundreths(double a)
    {
      double b = 0.0;
      b = a + POINT005;
      b *= HUNDRED;
      b = (int)b;
      b /= HUNDRED;
      return b;
    }
    
    double calcStartBalance(double start_b, double down_p)
    {
      return (roundToHundreths(start_b - down_p));
    }
    
    int main()
    {
       printf("%5.2lf\n", calcStartBalance(101, 5.43333));
       return 0;
    }
    P.S. This works too:
    Code:
    double roundToHundreths(double a)
    {
      return (int)((a + POINT005) * HUNDRED) / HUNDRED;
    }
    Last edited by dwhitney67; September 9th, 2010 at 11:32 PM.

  3. #3
    Join Date
    Jan 2010
    Beans
    137
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with C error 'conflicting types'

    I was able to get yours to compile, but mine still will not..Here is all the code I am trying to compile. I even broke it up smaller and tried to compile a segment the size of yours and that did not work either.

    Code:
    #include <stdio.h>
    #define HUNDRED 100.00
    #define POINT005 0.005
    
    
    int main(void)
    {
      double start_balance = 0.0;
      double down_payment = 0.0;
      double air = 0.0;
      double month_payment = 0.0;
      double new_balance = 0.0;
    
      printf("Enter the selling price: ");
      scanf("%lf", &start_balance);
    
      printf("Enter the down payment: ");
      scanf("%lf", &down_payment);
    
      printf("Enter the anual interest rate: ");
      scanf("%lf", &air);
    
      printf("Enter the monthly payment: ");
      scanf("%lf", &month_payment);
    
      new_balance = calcStartBalance(start_balance, down_payment);
      printf("The loan amount: %5.2lf\n", new_balance);
    
    }
    
    double roundToHundreths(double a)
    {
      double b = 0.0;
      b = a + POINT005;
      b *= HUNDRED;
      b = (int)b;
      b /= HUNDRED;
      return b;
    }
    
    double calcStartBalance(double start_b, double down_p)
    {
      return (roundToHundreths(start_b - down_p));
    }
    
    double canBePaid(double month_p, double interest)
    {
      return (roundToHundreths(month_p - interest));
    }
    
    double calcInterest(double air, double new_b)
    {
      return (roundToHundreths(air * new_b));
    }
    
    double calcNewBalance(double old_b, double to_b)
    {
      return (roundToHundreths(old_b - to_b));
    }
    p.s. - any criticisms you have about good coding practices I am not following are welcome
    Last edited by smdawson; September 9th, 2010 at 11:43 PM.
    Let books be your dining table, and you shall be full of delights,
    Let them be your mattress, and you shall sleep restful nights.

  4. #4
    Join Date
    Nov 2009
    Beans
    1,081

    Re: Help with C error 'conflicting types'

    It's probably 'problem unrelated', but your main() method declares an int return type without actually having a return statement. It also probably wouldn't hurt to add a function prototype (pref. in a header file) to avoid the compiler producing any implicit declarations.

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Help with C error 'conflicting types'

    Quote Originally Posted by Some Penguin View Post
    ... It also probably wouldn't hurt to add a function prototype (pref. in a header file) to avoid the compiler producing any implicit declarations.
    That's the ticket! The OP presented one thing in his opening post, then something different later.

    @ the OP: When you compile your source code, use the -Wall option and correct any/all warnings.

  6. #6
    Join Date
    Jan 2010
    Beans
    137
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with C error 'conflicting types'

    Sorry guys, I didn't know about prototyping the functions. After I did that it got rid of the errors.

    Also, should I use pointers for everything in my functions?
    like this example I found here

    Code:
    #include <stdio.h>
    
    f1(int *k);
    
    main ( ){
        int i;
    
        i = 0;
        printf (" The value of i before call %d \n", i);
        f1 (&i);
        printf (" The value of i after call %d \n", i);
    }
    
    f1(int *k)
    {
        *k = *k + 10;
    }
    Let books be your dining table, and you shall be full of delights,
    Let them be your mattress, and you shall sleep restful nights.

  7. #7
    Join Date
    Dec 2009
    Location
    Norway
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with C error 'conflicting types'

    Nope, F1 could just be passed values, then return its result as a value too, then the code after the call to F1 could assign, or not; thus using a functional style of programming as opposed to the imperative style seen in the example.

    (..I'm typing this on my phone; someone can probably show you a code example..)
    Last edited by worseisworser; September 10th, 2010 at 12:38 AM.
    Homepage | GitHub | Blog | Pay some of us a visit at #ubuntu-programming on FreeNode.

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Help with C error 'conflicting types'

    dawson,

    always show full prototypes; don't leave off the 'int' simply because it is implied by the compiler.

    Using a pointer for the example you provided is not esthetically pleasing. The following would have been better:
    Code:
    #include <stdio.h>
    
    int f1(int k);
    
    int main (){
        int i = 0;    // always initialize your variables
    
        printf (" The value of i before call %d \n", i);
        i = f1 (i);
        printf (" The value of i after call %d \n", i);
    
        return 0;
    }
    
    int f1(int k)
    {
        return k * 10;
    }

  9. #9
    Join Date
    Jan 2010
    Beans
    137
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Help with C error 'conflicting types'

    Ok. So always write full prototypes and I don't need to use pointers in the functions; I can just pass values. Thank you for all the help.
    Let books be your dining table, and you shall be full of delights,
    Let them be your mattress, and you shall sleep restful nights.

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: Help with C error 'conflicting types'

    Quote Originally Posted by smdawson View Post
    Ok. So always write full prototypes and I don't need to use pointers in the functions; I can just pass values. Thank you for all the help.
    Sometimes you will have to pass a pointer to a function; it just so happens that in the case you presented, it was not necessary.

    In other cases, such as when calling the stat() C library function, it is necessary. For example:
    Code:
    ...
    
    struct stat info;
    
    int result = stat("/some/file", &info);
    
    if (result == 0)
    {
       // structure 'info' has relevant information about the
       // file /some/file.
    }

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •