Like Tree3Likes
  • 1 Post By Click_here
  • 2 Post By yahzee

Floating point exception( core dump) error in my program..

This is a discussion on Floating point exception( core dump) error in my program.. within the C Programming forums, part of the General Programming Boards category; i m stuck at one problem...this is the program to print the given integer in words...plzz hep me to solve,,,, ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    14

    Unhappy Floating point exception( core dump) error in my program..

    i m stuck at one problem...this is the program to print the given integer in words...plzz hep me to solve,,,,

    Code:
    #include<stdio.h>
    main()
    {
     int num,temp1,temp2,cnt,temp3,temp4,a;
     printf("Enter the number=");
     scanf("%d",&num);
     temp1=1;
     cnt=-1;
     temp3=num;
     while(temp3)
     {
      temp2=temp3;
      while(temp2)
     {
      temp2=temp2/10;
      cnt++;
     }
     a=cnt;
     while(a)
     {
      a--;
      temp1=temp1*10;
     }
     temp4=temp3/temp1;
     switch(temp4)
     {
      case 1:
      printf("one ");
      break;
      case 2:
      printf("two ");
      break;
      case 3:
      printf("three ");
      break;
      case 4:
      printf("four ");
      break;
      case 5:
      printf("five ");
      break;
      case 6:
      printf("six ");
      break;
      case 7:
      printf("seven ");
      break;
      case 8:
      printf("eight ");
      break;
      case 0:
      printf("nine ");
      break;
      default:
      printf("zero ");
     }
     
     temp3=temp3%temp1;
     }
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Wow man. Try to wright your program with indents, and with proper new lines. This is hardly readable. Try like this:


    Code:
    int num,temp1,temp2,cnt,temp3,temp4,a;
        
        printf("Enter the number=");
        scanf("%d",&num);
        
        temp1=1;
        cnt=-1;
        temp3=num;
        
        while(temp3)
        {
            temp2=temp3;
            while(temp2)
            {
                temp2=temp2/10;
                cnt++;
            }
            a=cnt;
            while(a)
            {
                a--;
    and so on.
    Do you even now what you wrote in your program?

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Can you please copy paste the error you get?

    However if I input zero, no output will be produced.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Try to use do while instead of while, because if user inputs zero program will evaluate condition in while as false and skip while loop.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    i cannot use do while loop because this is my assignment using while loop......can anyone give me solution of this error..

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Your case 0 says "nine"

    But beside that, you are getting that error when temp1 is 0 and you try to divide something by it.
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,549
    3 lines from end you are using the "%" operator; why?

    Code:
    temp3=temp3%temp1;
    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    Quote Originally Posted by stahta01 View Post
    3 lines from end you are using the "%" operator; why?

    Code:
    temp3=temp3%temp1;
    Tim S.
    dude i m using logic here
    let take example with num=456
    and i need at output is four five six
    first i divide 456/100=4 then print
    now i need 56/10=5
    thats why i use 456%100=56
    after that agai loop
    thats why i use
    Code:
     
    temp3=temp3%temp1;

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    14
    Quote Originally Posted by Click_here View Post
    Your case 0 says "nine"

    But beside that, you are getting that error when temp1 is 0 and you try to divide something by it.
    dude temp1 can never be zero because i initialize temp1=1....

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,549
    Edited: Changed the word "logic" to "implementation"; because logic was OK. Coding was not OK.
    No you are using poor implementation here.

    These lines to initialize the vars for the while loops are only done once!!

    temp1=1;
    cnt=-1;

    They need to be done in the correct place!

    If you describe the problem or used real descriptive variable names it would be easier to figure out your issue.

    I suggest adding these lines before the line your change temp3; or use a debugger.
    Code:
            printf("temp1:= %d \n", temp1);
            printf("temp2:= %d \n", temp2);
            printf("temp3:= %d \n", temp3);
            printf("temp4:= %d \n", temp4);
    Edit: After I fixed the other mistakes this line did work OK "temp3=temp3%temp1;" but till I fixed the temp1 calculation it was an endless loop.

    Tim S.
    Last edited by stahta01; 01-20-2013 at 10:30 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,300
    Well line 51 is an obvious mistake.

    Other than that, the main problem here is your lack of using a debugger. You need to learn to use that - you won't get far without it. Ask us how we can help you with that, and provide details as to what you have available.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,344
    Quote Originally Posted by nareshlove View Post
    dude temp1 can never be zero because i initialize temp1=1....
    Well... not in the first loop

    But I put an "assert" in your code earlier today and ran it, and yes it does become 0.

    Code:
    #include <assert.h>
    
    ...
    
    assert(temp1 != 0);
    temp4=temp3/temp1;
    It fails there after a few loops.
    stahta01 likes this.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    42
    Quote Originally Posted by nareshlove View Post
    i cannot use do while loop because this is my assignment using while loop......can anyone give me solution of this error..
    Then you should use if before while if you need to printf zero for input 0.

    Also try writing new program from beggining, do it step by step. At beginning do not use tons of variables, insert them only when you really need them. This names temp1, temp2, temp3 can really be confusive, so give them descriptive names. And also after every step use printf or debugger to see where are you going.
    AndiPersti and stahta01 like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime #'s question. "Floating point exception" error.
    By andrew89 in forum C Programming
    Replies: 16
    Last Post: 12-29-2011, 08:37 AM
  2. Floating Point Exception error
    By Mentallic in forum C Programming
    Replies: 5
    Last Post: 08-27-2011, 01:40 AM
  3. Where's my program's problem? (Floating point exception?)
    By readytogo in forum C Programming
    Replies: 2
    Last Post: 10-28-2010, 03:48 PM
  4. Floating Exception (Core Dumped)
    By DarrenY in forum C Programming
    Replies: 9
    Last Post: 05-14-2007, 10:01 AM
  5. floating point exception? what causes these?
    By salvelinus in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2002, 12:12 PM

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