Hey I have a segmentation fault and can't locate it,maybe one of you can.
Main:
Code:
#include <queue>
#include <iostream>
#include <fstream>
#include <string>
#include "rider.h"
using namespace std;
int main()
{
//variable used to count the ticket
int tickets;
//variable used to keep time
int clock;
//tmp variable used in loops as counter
int i;
//queue that the information from the infile gets stored into
queue<rider> arrivals;
//queue that the riders with the correct clock time get put into
queue<rider> line;
//queue that the rider that needs to be put back in line gets held
queue<rider> backIn;
//string that makes it easier to remember the name of the file to open
//to read from
string inFileName = "riders.dat";
//string that makes it easier to remember the name of the file to open
//to write to
string outFileName = "riders.out";
//file read from
ifstream inFile;
//file write to
ofstream outFile;
//opens the file to read from
inFile.open(inFileName.c_str());
//opens the file to write to
outFile.open(outFileName.c_str());
//sets the clock to 0
clock = 0;
//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))
{
//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);
}
//once all the customers are read from the inFile and put into the arrivals
//queue the program and clock will have to be run where this data will be
//processed. For the processing part of the program, it will run while
//the queues arrivals and line are not empty, therefore they will stop
//running when they become empty.
while(!(arrivals.empty() == true && line.empty() == true && backIn.empty() == true))
{
//makes transfer a variable of type rider equal to the front of the
//arrivals queue
rider transfer = arrivals.front();
//this will look to see if the transfer arrival time is equal to 0, if not
//it will set clock equal to that number then copy all the transfers that
//have that clock number.
//checks to see if the transfer time is equal to the clock time
if(!line.empty())
{
//will put all the customers with the same arrival
//time as the clock into the line queue
while(transfer.time == clock)
{
//here the front of the arrivals queue is put into the line queue
line.push(transfer);
//here the old front from the arrivals queue is popped off
arrivals.pop();
}
//need to check to see if backIn is empty or not, if it is
//then it won't add anything, if not it will add the customers data
if(!backIn.empty())
{
//if the rider from the previous time still has tickets left it
//will be added after the next group of customers from that time
line.push(backIn.front());
}
//after the customers are added to the line queue the ride
//will begin to run, which entails taking away one ticket
//from the person on the ride then checking to see if any
//tickets are left and if so add to the end of the line queue
//after the next group of customers enter, if no tickets are
//left then write out the name and the clock time to the out file.
//creates a variable onRide to represent the front of the line
//queue which represents that customer going on the ride
rider onRide = line.front();
//takes 1 ticket way from the customer that is going on the line
onRide.tickets--;
//increments clock by 1 unit
clock++;
//after the ride is completed it checks to see if the customer
//that was just on the ride has more tickets to go back in line
//or if it should be written to the outfile
//if the customer on ride has 0 tickets the last name and the
//clock time get printed out on the line to the outfile
if(onRide.tickets == 0)
{
//write the customers lastName and clock time to the outfile
outFile << onRide.lastName << clock << endl;
}
//else if the cusomters ticket count is greater than 0 the customer
//and his info will get stored into backIn to place the customer
//back in line
else
{
//places the customer on the ride's data into the backIn
//queue so they may get placed back in line
backIn.push(onRide);
}
}
//else if the first customers time is not equal to the clock it
//sets clock equal to that customers time then continues on
else
{
//sets clock equal to the first customers time
clock = transfer.time;
//will put all the customers with the same arrival
//time as the clock into the line queue
while(transfer.time == clock)
{
//here the front of the arrivals queue is put into the line queue
line.push(arrivals.front());
//here the old front from the arrivals queue is popped off
arrivals.pop();
}
//need to check to see if backIn is empty or not, if it is
//then it won't add anything, if not it will add the customers data
if(!backIn.empty())
{
//if the rider from the previous time still has tickets left it
//will be added after the next group of customers from that time
line.push(backIn.front());
}
//after the customers are added to the line queue the ride
//will begin to run, which entails taking away one ticket
//from the person on the ride then checking to see if any
//tickets are left and if so add to the end of the line queue
//after the next group of customers enter, if no tickets are
//left then write out the name and the clock time to the out file.
//creates a variable onRide to represent the front of the line
//queue which represents that customer going on the ride
rider onRide = line.front();
//takes 1 ticket way from the customer that is going on the line
onRide.tickets--;
//increments clock by 1 unit
clock++;
//after the ride is completed it checks to see if the customer
//that was just on the ride has more tickets to go back in line
//or if it should be written to the outfile
//if the customer on ride has 0 tickets the last name and the
//clock time get printed out on the line to the outfile
if(onRide.tickets == 0)
{
//write the customers lastName and clock time to the outfile
outFile << onRide.lastName << clock << endl;
}
//else if the cusomters ticket count is greater than 0 the customer
//and his info will get stored into backIn to place the customer
//back in line
else
{
//places the customer on the ride's data into the backIn
//variable so they may get placed back in line
backIn.push(onRide);
}
}
}
}
//closes the infile
inFile.close();
//closes the outfile
outFile.close();
//signals ok to the os
return 0;
};
riders.h:
Code:
#include<string>
using namespace std;
//this is a struct named Rider that will be used to store all the data of a
//rider on the fair ride, this makes it easier so that I can make a queue of
//type rider instead of multiple queues for each data type of data
struct rider
{
//variable for last name of rider
string lastName;
//variable for number of tickets the rider has
int tickets;
//variable for the arrival time of the rider
int time;
};