Calculations problem...?

This is a discussion on Calculations problem...? within the C Programming forums, part of the General Programming Boards category; It's really basic. Sorry for posting the whole program, but I have no idea where I've gone wrong. While executing ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    16

    Calculations problem...?

    It's really basic. Sorry for posting the whole program, but I have no idea where I've gone wrong.
    While executing the bold statement below: If I type 100000 as the policy amount, I get -124 as the output. It should be 400. What have I done?? :-(

    Code:
    #include<stdio.h>
    int main(void)
    {
      char hlth, resi, sex;
      int age,pol_amt;
      float prem;
    
      printf("Enter health grade, residence, sex, age:");
      scanf( "%c %c %c %d", &hlth, &resi, &sex, &age);
    
      if( hlth=='E' && (age>=25 && age<=35) && resi=='c')
    
      {
         printf("\nThe person can be insured.");
         if( sex=='M' )
         {
            printf("\nThe maximum policy amount is 200000");
            printf("\nEnter the policy amount:");
            scanf("%d", &pol_amt);
    
            prem= (pol_amt/1000)*4;
    
            printf("\nThe premium rate is %f",prem);
         }
        else if( sex=='F' )
        {
           printf("\nThe maximum policy amount is 100000");
           printf("\nEnter the policy amount:");
           scanf("%d", &pol_amt);
    
           prem=(pol_amt/1000)*3;
          printf("\nThe premium rate is %f",prem);
        }
      }
      else if( hlth=='P' && (age>=25 && age<=35) && resi=='v' && sex=='M')
      {
         printf("\nThe person can be insured.");
         printf("\nThe maximum policy amount is 10000.");
         printf("Enter the policy amount:");
         scanf("%d", &pol_amt);
    
         prem=(pol_amt/1000)*6;
         printf("\nThe premium rate is %f",prem);
      }
      else
      {
        printf("\nThe person is not insured.");
      }
    
    getch();
    clrscr();
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Don't know what you are doing wrong, as I get 400.0000000. [But you are not returning anything at the end of main, you should add return 0].


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    16
    Oh dear... I typed in a return 0, but still I get -124.00000 as the answer... Why exactly do I need a return 0? I've made programs without it and I've had no problems...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Beachblue View Post
    Oh dear... I typed in a return 0, but still I get -124.00000 as the answer... Why exactly do I need a return 0? I've made programs without it and I've had no problems...
    Because your program is supposed to give a value back.

    I believe (after trying it out) that your problem is that "int" is only 16 bits (perhaps you are using the old Turbo C compiler, rather than a modern compiler?), which makes 100000 an overflow, so the resulting value is -31072, and of course, 4*-31 -> -124.

    Either use a compiler that isn't so old that it would be be good if it was the age of Whiskey, or change your int to either long int, or float/double.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    16
    Thanks, using float did the trick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. array problem?
    By ssjnamek in forum C Programming
    Replies: 14
    Last Post: 02-08-2006, 05:03 PM

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