Segmentation Fault :(

This is a discussion on Segmentation Fault :( within the C++ Programming forums, part of the General Programming Boards category; i realize they are stupid my professor wants them. This is completely legit asking for help i've spent hours trying ...

  1. #16
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i realize they are stupid my professor wants them. This is completely legit asking for help i've spent hours trying to find the problem.

  2. #17
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
        //creates customer of the struct rider;
        rider customer;
    
        //while the infile can be read from
        while(inFile)
        {
    
        //it will loop and read from the inFile data while the customer data
        //is not equal to END 0 0 
        while(!(customer.lastName == "END" && customer.tickets == 0 && customer.time ==0))
        {
    you havent done anything with 'customer' so why compare its fields? lastName will be empty string (i think) and the two ints will be undefined (likely some random number left over from the memory at that address). this code runs, however there is obviously some logic error. maybe you want a do-while loop? you may need to look over your other loops and consider a do-while loop instead.

    edit: also instead of while(inFile) use while(inFile.good())
    Last edited by nadroj; 04-05-2007 at 11:25 PM.

  3. #18
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    so then how else am i going to get the customer data to be read in?

  4. #19
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
       while(inFile.good())
        {
    
        //it will loop and read from the inFile data while the customer data
        //is not equal to END 0 0 
        do 
        {
            //here the file will place the correct data for each customer into 
            //the correct variables
            inFile >> customer.lastName >> customer.tickets >> customer.time;
    
    	
            //after the customer in that particular read is done putting it in the
            //correct variables the customer gets pushed ito arrivals queue
            arrivals.push(customer);
        }while(!(customer.lastName == "END" && customer.tickets == 0 && customer.time ==0));
    works fine, ive tested this loop and printed out what it reads each time and it matches the .dat file. look over your other loops

  5. #20
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    ok so r u saying that the error is somewhere else now? or the program runs completely?

  6. #21
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the program still never exists for me. im saying that first do loop is fine (at least the one i tried, as i posted above).

    look over the logic of your other loops.. others may need to be changed to do...while loops, maybe not though havent looked them over. also be careful with all of your conditions and using !not, is there a logic error there? must be somewhere down there.

    the inifinite loop occurs within this while loop:
    Code:
    while(!(arrivals.empty() == true && line.empty() == true && backIn.empty() == true))
    most likely meaning the condition your checking here isnt right.. is it?
    Last edited by nadroj; 04-05-2007 at 11:35 PM.

  7. #22
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    isn't ! the same thing as writing !=? and could you help me identify the loop causing the problem i've spent hours on this and can't see it.

  8. #23
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    If you also post the example input file which makes it crash for you, then we'd be able to replicate what you're doing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #24
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    example input file was posted little while ago, name it 'riders.dat'
    Hopper 2 1 Torvalds 3 10 Jobs 8 4 Gates 100 100 END 0 0
    is there ever a time where that while loop will be false? let me try and rewrite it
    while(!(arrivals.empty() == true && line.empty() == true && backIn.empty() == true))
    the above is _always_ true, so:

    while(arrivals.empty() == true && line.empty() == true && backIn.empty() == true)
    the above is _always_ false, so, removing the '== true', we have:

    while(arrivals.empty() && line.empty() && backIn.empty())
    the above is _always_ false
    therefore all 3 of these will never be empty at the same time.. either your checking the wrong condition, or you have some logic error somewhere that is preventing this condition from being true.

    your code is somewhat difficult to follow, which is making this difficult to debug.. i would have to print your code and sit down and try and understand it, or read the assignment sheet (if any) to understand what you are _trying_ to do.
    Last edited by nadroj; 04-05-2007 at 11:48 PM.

  10. #25
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    Quote Originally Posted by DarkDot View Post
    riders.h didn't change and same code is as follows in a riders.dat file that the program writes to the outfile riders.out
    Code:
    Hopper 2 1 Torvalds 3 10 Jobs 8 4 Gates 100 100 END 0 0
    i did thats it right there

  11. #26
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    i just want it to loop through while all of the queues still have stuff in them, thats now what that first statement says? I mean how else would i have the loops set up to do what i want? i can't think of a way.
    Last edited by DarkDot; 04-05-2007 at 11:51 PM.

  12. #27
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    it does say that.

    your original condition:
    Code:
    while(!(arrivals.empty() == true && line.empty() == true && backIn.empty() == true))
    (in your code/logic) this condition is always true.

    the above is the same as saying:
    Code:
    while(!(arrivals.empty() && line.empty() && backIn.empty()))
    which is the same as saying:
    Code:
    while(!arrivals.empty() && !line.empty() && !backIn.empty())
    again, this is always true.

    what this means is that there is never a time in your code that will make this condition false. arrivals, line, and backin will never be empty.

    again, your while loop does say what you want it to, however, it results in an infinite loop. so the condition is never false, so its some logic your doing within this while loop.. somewhere in all those ifs and pops and pushes, which i wont go through and figure out what your supposed to be doing.

    re-read your assignment sheet or whatever this is for/from to see what your supposed to do there.
    Last edited by nadroj; 04-06-2007 at 12:01 AM.

  13. #28
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    won't they be empty when all the data is written to the outfile?

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,326
    Um, nadroj, I think you did not get your De Morgan's theorem correct.
    Code:
    while(!(arrivals.empty() == true && line.empty() == true && backIn.empty() == true))
    is indeed equivalent to
    Code:
    while(!(arrivals.empty() && line.empty() && backIn.empty()))
    But now the above is equivalent to:
    Code:
    while(!arrivals.empty() || !line.empty()|| !backIn.empty())
    Last edited by laserlight; 04-06-2007 at 12:11 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    writing data to the outfile has nothign to do with the queues being empty or not, so no, writing as much or as little to the outfile wont make them empty.

    again, look at your logic.. heres another one:
    Code:
    while(!(arrivals.empty() == true && line.empty() == true && backIn.empty() == true))
        {
            rider transfer = arrivals.front();
        
            // HERE
            if(!line.empty())
    // why check if !line.empty()? you already check whether line.empty() is true or false, which is why this while loop is being executed.
            {
                while(transfer.time == clock)
                {
                line.push(transfer);
                arrivals.pop();
                }
    
             // HERE
                if(!backIn.empty())
    // same as above
                {...

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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