Thread: floating point exception

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    1

    Unhappy floating point exception

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    int main(int argc, char *argv[]){
        char *line;
        int number_words, number_space, size_line;
        int space_per_word, upoloipo, i, j;
        
        size_line = atoi (argv[1]);
        
        line = (char*) malloc(sizeof(char)*size_line);
        if ( line == NULL ) {
            return 1;
        }
        
        for(i=0; i<size_line; i++){
            line[i] = '\0';
        }
        do {
            scanf("%d %d ", &number_words, &number_space);
            if ( number_words != 0 ) {
                space_per_word = number_space / (number_words - 1 );
                upoloipo = number_space % (number_words - 1 );
                for (i=1; i<=number_words; i++){
                    scanf("%s", line);
                    if (i < number_words ){
                        for (j=0; j<space_per_word +1; j++){
                            strcat(line, " ");
                        }
                        if (upoloipo >0){
                            strcat (line, " ");
                            upoloipo = upoloipo - 1;
                        }
                    }
                    else{
                        strcat (line, "\n");
                    }
                    printf ("%s", line);
                }
            }
        }while ( number_words != 0 );
        
        return (0);
    }
    this programm is called p3
    i have a file that contains :
    3 3 There is a 2 2 lovely road 2 4 that runs 2 3 from Ixopo 2 5 into the 2 1 hills. These 2 4 hills are 1 0 grass-covered 2 1 and rolling, 3 1 and they are 2 0 lovely beyond 2 2 any singing 3 3 of it. The 2 2 road climbs 2 2 seven miles 3 0 into them, to 1 1 Carisbrooke; 2 5 and from 2 4 there, if 3 2 there is no 2 4 mist, you 3 1 look down on 3 3 one of the 1 6 fairest 2 3 valleys of 1 6 Africa. 0

    then :
    gcc -Wall -g p3.c -o p3

    and
    ./p3 13 < p2test

    when i run it , it prints :
    There is a
    lovely road
    that runs
    from Ixopo
    into the
    hills. These
    hills are
    Floating point exception (core dumped)

    what i did wrong ???

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I suggest you run your program through a debugger, finds where it crashes, and check the value of your variables at that point.

    Hint: If you look carefully at the following lines of code, you should see why the problem is occurring:

    Code:
    if ( number_words != 0 ) {
        space_per_word = number_space / (number_words - 1 );
    Additional notes:

    • You should check that the correct number of arguments are passed to your program (i.e. "argc") before attempting to access "argv"
    • Don't cast "malloc()": FAQ > Casting malloc - Cprogramming.com
    • Free the memory that you allocate

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Floating point exception
    By jayalakshmi in forum C Programming
    Replies: 5
    Last Post: 10-31-2012, 12:28 PM
  2. Floating Point Exception
    By Mentallic in forum C Programming
    Replies: 2
    Last Post: 08-12-2011, 06:48 AM
  3. floating point exception?
    By snowball in forum C Programming
    Replies: 2
    Last Post: 03-04-2011, 10:45 AM
  4. floating point exception
    By megastar in forum C Programming
    Replies: 6
    Last Post: 07-09-2007, 04:22 AM
  5. Floating point exception ????
    By wuzzo87 in forum C Programming
    Replies: 3
    Last Post: 04-25-2007, 02:38 AM