Here is my entire code. I am getting a segmentation fault ( I love pointers for this reason!!!!) off of one of my head pointers i believe.

Can anyone help me find/ fix this? please, and thank you.

/*
The purpose of the program is to simulate a queued line at an amusement park for 
the new, very popular ride.  This program will run through a 10 hour day and 
randomly calculate how many males, females, a variety of ages, and how many
satisfied riders there were out of the total amount.  The wait time the riders
find acceptable will be randomly measured.  The final information will print
onto the screen and also be sent to the following data file:

queueline.dat

*/

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

struct people {
       
       // struct for defining the stats to be measured
       people * next;
       int gender;
       int age;
       int timeWaited;
       int okWaitTime;
       
} person ;

class queue {
      
      public:
      
             queue ();
             void enqueue(int &, int &, int &, int &, int &, int &, 
                    int &, int &, int &);
             void dequeue(int &, int &, int &, int &);
             void addtime();
                          
      private:
              
              int totalRiders;
              int totalWaitTime;
              int totalAge;
              // calculates the totals respectively
              int highest, lowest;
              int range;
              // used for randomization
              int min;
              // time function
              int male, female;
              // counts number of males and females
              int happyWithWait;
              // counts how many satisfied and unsatisfied people there are
              int teens, twenties, thirties, forties, fifties, sixties, seventies;
              // these values will keep track of how many people fit in each age group
              int joining;
              // the actual amount of people joining will vary depending on time of day
              string gender;
              people * head;
              people * tail;
                           
};

queue::queue () {
             
             // constructor to start all variables out as zero or null
             
             male = 0, female = 0;
             happyWithWait = 0;
             teens = 0, twenties = 0, thirties = 0, forties = 0,
                  fifties = 0 , sixties = 0, seventies = 0;
             head = NULL;
             tail = NULL;
}

void queue::enqueue (int &male, int &female, int &teens, int &twenties, int &thirties,
                         int &forties, int &fifties, int &sixties, int &seventies) {
     
     
     // Enqueue function will add a random number of people to the line at different time
     // All variables are passed as call by reference so their values can be printed out 
     // in the main function
     
     srand((unsigned)time(0));
     
     if (head == NULL) {
         
            people * temp = new people;
            if (min <= 119) { 
              // this will be the first 2 hours of the day
              // starting at 10 am this will take you to 12 pm
              // there will be less riders at this point in the day
              
              lowest = 5;
              highest = 30;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
                // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
        }
                
        else if (min > 119 && min <= 239) {
              // time covered here will be from 12 pm until 2 pm
              // more riders will join here than the previous time
              
              lowest = 5;
              highest = 35;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
              
                // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                if (temp -> age < 20)
                          teens = teens + 1;
                     else if (temp -> age >= 20 && temp -> age < 30) 
                          twenties = twenties + 1;
                     else if (temp -> age >= 30 && temp -> age < 40)  
                          thirties = thirties + 1;
                     else if (temp -> age >= 40 && temp -> age < 50)
                          forties = forties + 1;
                     else if (temp -> age >= 50 && temp -> age < 60)
                          fifties = fifties + 1;
                     else if (temp -> age >= 60 && temp -> age < 70)
                          sixties = sixties + 1;
                     else 
                          seventies = seventies + 1;
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                
             
        }
        else if (min > 239&& min <= 419 ) {
              // time here is from 2pm until 5 pm
              // The most riders will join during this time
              
              lowest = 10;
              highest = 50;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
                 
                // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                if (temp -> age < 20)
                          teens = teens + 1;
                     else if (temp -> age >= 20 && temp -> age < 30) 
                          twenties = twenties + 1;
                     else if (temp -> age >= 30 && temp -> age < 40)  
                          thirties = thirties + 1;
                     else if (temp -> age >= 40 && temp -> age < 50)
                          forties = forties + 1;
                     else if (temp -> age >= 50 && temp -> age < 60)
                          fifties = fifties + 1;
                     else if (temp -> age >= 60 && temp -> age < 70)
                          sixties = sixties + 1;
                     else 
                          seventies = seventies + 1;
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                
        }
        else {
              //this will be from 5pm until close at 8pm
              // a good majority will join here due to kids
              // with season passes joining the line after school
              // or parents taking kids after work
              // joining will be less than 2pm til 5pm and will rival
              // 12 pm til 2 pm.  Should be greater than the early
              // morning rush
              
              lowest = 5;
              highest = 35;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
               // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                if (temp -> age < 20)
                          teens = teens + 1;
                     else if (temp -> age >= 20 && temp -> age < 30) 
                          twenties = twenties + 1;
                     else if (temp -> age >= 30 && temp -> age < 40)  
                          thirties = thirties + 1;
                     else if (temp -> age >= 40 && temp -> age < 50)
                          forties = forties + 1;
                     else if (temp -> age >= 50 && temp -> age < 60)
                          fifties = fifties + 1;
                     else if (temp -> age >= 60 && temp -> age < 70)
                          sixties = sixties + 1;
                     else 
                          seventies = seventies + 1;
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                
        }
            
            temp -> next = NULL;
            head = tail = temp;

     }
     
     else {
          
            people * temp = new people;
            if (min <= 119) { 
              // this will be the first 2 hours of the day
              // starting at 10 am this will take you to 12 pm
              // there will be less riders at this point in the day
              
              lowest = 5;
              highest = 30;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
                // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                if (temp -> age < 20)
                          teens = teens + 1;
                     else if (temp -> age >= 20 && temp -> age < 30) 
                          twenties = twenties + 1;
                     else if (temp -> age >= 30 && temp -> age < 40)  
                          thirties = thirties + 1;
                     else if (temp -> age >= 40 && temp -> age < 50)
                          forties = forties + 1;
                     else if (temp -> age >= 50 && temp -> age < 60)
                          fifties = fifties + 1;
                     else if (temp -> age >= 60 && temp -> age < 70)
                          sixties = sixties + 1;
                     else 
                          seventies = seventies + 1;
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
        }
                
        else if (min > 119 && min <= 239) {
              // time covered here will be from 12 pm until 2 pm
              // more riders will join here than the previous time
              
              lowest = 5;
              highest = 35;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
              
                // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                if (temp -> age < 20)
                          teens = teens + 1;
                     else if (temp -> age >= 20 && temp -> age < 30) 
                          twenties = twenties + 1;
                     else if (temp -> age >= 30 && temp -> age < 40)  
                          thirties = thirties + 1;
                     else if (temp -> age >= 40 && temp -> age < 50)
                          forties = forties + 1;
                     else if (temp -> age >= 50 && temp -> age < 60)
                          fifties = fifties + 1;
                     else if (temp -> age >= 60 && temp -> age < 70)
                          sixties = sixties + 1;
                     else 
                          seventies = seventies + 1;
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                
             
        }
        else if (min > 239&& min <= 419 ) {
              // time here is from 2pm until 5 pm
              // The most riders will join during this time
              
              lowest = 10;
              highest = 50;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
                 
                // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                if (temp -> age < 20)
                          teens = teens + 1;
                     else if (temp -> age >= 20 && temp -> age < 30) 
                          twenties = twenties + 1;
                     else if (temp -> age >= 30 && temp -> age < 40)  
                          thirties = thirties + 1;
                     else if (temp -> age >= 40 && temp -> age < 50)
                          forties = forties + 1;
                     else if (temp -> age >= 50 && temp -> age < 60)
                          fifties = fifties + 1;
                     else if (temp -> age >= 60 && temp -> age < 70)
                          sixties = sixties + 1;
                     else 
                          seventies = seventies + 1;
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                
        }
        else {
              //this will be from 5pm until close at 8pm
              // a good majority will join here due to kids
              // with season passes joining the line after school
              // or parents taking kids after work
              // joining will be less than 2pm til 5pm and will rival
              // 12 pm til 2 pm.  Should be greater than the early
              // morning rush
              
              lowest = 5;
              highest = 35;
              range = highest - lowest + 1;
              joining = lowest + int(range*rand()/(RAND_MAX + 1.0));
              
               // This function is used to get the gender of the person
    
                temp -> gender = (rand()%2)+1;
                if (temp -> gender == 1) {
                   gender = "male";
                   male = male + 1;
                }
                else {
                   gender = "female";
                   female = female + 1;
                }
                   
                // This will get the age of the person
                // Maximum age is 79 years old
                // Minimal age is 10 years old
                
                highest = 79;
                lowest = 10;
                range = highest - lowest + 1;
                temp -> age = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                if (temp -> age < 20)
                          teens = teens + 1;
                     else if (temp -> age >= 20 && temp -> age < 30) 
                          twenties = twenties + 1;
                     else if (temp -> age >= 30 && temp -> age < 40)  
                          thirties = thirties + 1;
                     else if (temp -> age >= 40 && temp -> age < 50)
                          forties = forties + 1;
                     else if (temp -> age >= 50 && temp -> age < 60)
                          fifties = fifties + 1;
                     else if (temp -> age >= 60 && temp -> age < 70)
                          sixties = sixties + 1;
                     else 
                          seventies = seventies + 1;
                
                // This will calculate acceptable time waited
                // Maximum acceptable time will be 300 minutes
                // Minimal acceptable time will be 15 minutes
                
                highest = 600;
                lowest = 15;
                range = highest - lowest + 1;
                temp -> okWaitTime = lowest + int(range*rand()/(RAND_MAX + 1.0));
                
                
        }
            
            head -> next = temp;
            temp -> next = NULL;
            head = temp;
     
     }
}

void queue::dequeue (int &happyWithWait, int &totalRiders, int &totalAge, int &totalWaitTime) {
     
     // Dequeue function is for putting someone onto the ride and removing them from the line
     // All variables are passed as call by reference so that they can be printed out in the
     // main function
     
     int x;
     for (x = 0; x < 20; x++) {
            people * temp = tail;
            if (head == tail) {
                     totalRiders++;
                     totalAge = totalAge + temp -> age;
                     totalWaitTime = totalWaitTime + temp -> timeWaited;
                     if(temp -> timeWaited <= temp -> okWaitTime)
                            happyWithWait++;
                     head = NULL;
                     tail = NULL;            
            }
            else {
                     totalRiders++;
                     totalAge = totalAge + temp -> age;
                     totalWaitTime = totalWaitTime + temp -> timeWaited;
                     if(temp -> timeWaited <= temp -> okWaitTime)
                            happyWithWait++;
                     tail = tail -> next;
                     delete temp;
            }
     }
}

void queue::addtime () {
     
     // This function just adds a minute to the time waited
     
     people * temp = tail;
	 
     while(temp != head) {
            temp -> timeWaited = temp -> timeWaited + 1;
		    temp = temp -> next;
      }
}

int main () {
    
    queue ride;
    people person;
    string gender;
    int happyWithWait, totalRiders, totalAge, totalWaitTime;
    int male, female;
    int teens, twenties, thirties, forties, fifties, sixties, seventies;
    
    cout << "This is a line simulator to determine how happy people " << endl;
    cout << "will be after waiting to board the ride." << endl << endl;
    
    for (int min = 1; min <= 600; min++) {
        // for loop for 600 minutes = 10 hours of the day
        // starting at around 10 AM until 8 PM
        // anyone in line after the 600 minutes will be kicked out of line
        
        ride.enqueue (male, female, teens, twenties, thirties, forties, fifties,
                     sixties, seventies);
        ride.dequeue (happyWithWait, totalRiders, totalAge, totalWaitTime);
        ride.addtime ();
           
    }
    
    cout << "Total amount of riders: " << totalRiders << endl;
    cout << "There were: " << male << " males and " << female << " females." << endl;
    cout << endl << "Age breakdown: " << endl;
    cout << "There were " << teens << " teenagers. " << endl;
    cout << "There were " << twenties << " twenty year olds." << endl;
    cout << "There were " << thirties << " thirty year olds." << endl;
    cout << "There were " << forties << " forty year olds." << endl;
    cout << "There were " << fifties << " fifty year olds." << endl;
    cout << "There were " << sixties << " sixty year olds." << endl;
    cout << "There were " << seventies << " seventy year olds." << endl;
    cout << "The average age was " << totalAge/totalRiders << "." << endl;
    cout << endl << happyWithWait << " were happy with their wait time." << endl;
    cout << "While " << totalRiders - happyWithWait << " were unhappy with wait time." << endl;
    cout << "The average wait time was " << totalWaitTime/totalRiders << "." << endl;
    
    
    cin >> teens;


return 0;
}

Recommended Answers

All 2 Replies

As you are saying it is segfaulting, I assume you are on the Linux platform (or a Unix like platform)

In that case you could use gdb, the GNU debugger, to locate the problem.

----------------------

Programming ( Assignment / Project ) Help

As you are saying it is segfaulting, I assume you are on the Linux platform (or a Unix like platform)

actually i use dev-c++ on windows but i know how it acts when i get a seg fault.. i also use g++ on linux to compile after my windows version is working correctly just to double check (not always working the same)

I did get the problem worked out. and thank you proghelper for your reply :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.