Thread: Segmentation fault: 11?

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    11

    Segmentation fault: 11?

    I'm getting a segmentation fault:11 after I run the code. I'm new to programming and not sure what the fault is. Any help would be appreciated.

    My code is:

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <time.h>
    
    
    
    
     //main function
    int main() {
        int i, x;
        int num_bids;
        int num_auctions;
    
    
        float bid;
        float max;
        float sum = 0;
    
    
    
    
        //open input file
        FILE * ifp = fopen("/Users/ninagonzalez/Desktop/COP3223/bids.txt" , "r");
        //open output file
        FILE * ofp = fopen("/Users/ngonzalez/Desktop/output.txt" , "w");
        //scan in info
        fscanf(ifp, "%d" , &num_auctions);
    
    
        for(i=0; i<num_auctions; i++){
    
    
            fscanf(ifp, "%d",&num_bids);
                for (x = 0; x < num_bids;x++)
    
    
                {
    
    
                fscanf(ifp, "%d", &num_bids);
                max = bid;
    
    
                }
    
    
    
    
            for(i=1; i<num_auctions; i++)
                if (bid>max)
                max = bid;
    
    
        }
    
    
        max=0;
    
    
        //sum
        sum += bid;
    
    
         // print out to the output
        fprintf(ofp,"Auction 1 was sold for $%.2f\n", bid);
        fprintf(ofp,"Auction 2 was sold for $%.2f\n", bid);
        fprintf(ofp,"Auction 3 was sold for $%.2f\n", bid);
        fprintf(ofp,"Auction 4 was sold for $%.2f\n", bid);
        fprintf(ofp,"Auction 5 was sold for $%.2f\n", bid);
        fprintf(ofp, "The total made for charity is $%.2f ", bid);
        // close
        fclose(ifp);
        fclose(ofp);
    
    
    
    
    
    
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2013
    Posts
    11
    Well I posted that post pre-maturly I found the error. But the auctions are not adding up?

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    For one you're using 'bid' without ever setting it or initializing it. I'm guessing you're supposed to read that in somewhere at some point?
    The whole thing is a bit shaky inherently though due to the nature of the repeated fscanf() inside loops and no error checking.

    You want to ensure that you're fscanf() calls are actually succeeding (at the very least) but really I would suggest using the classic fgets()+sscanf() pair for parsing this kind of data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault ?
    By jigamuffin in forum C Programming
    Replies: 3
    Last Post: 05-01-2012, 02:31 AM
  2. segmentation fault
    By muppy in forum C Programming
    Replies: 10
    Last Post: 06-05-2011, 05:28 PM
  3. Help with segmentation fault!!!
    By Tiago Ferreira in forum C Programming
    Replies: 3
    Last Post: 05-31-2011, 07:34 AM
  4. Segmentation fault
    By Buckshot in forum C++ Programming
    Replies: 14
    Last Post: 06-23-2005, 08:20 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM