so i have this program written and i am getting a seg fault and the stack said in operator<<. I have no idea how to fix this...It is due in thirty minutes if someone can please help me asap.
Here is where i think the problem lies:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include "listT.h"
using namespace std;
struct purchase
{
string description;
float cost;
//Overloading equality(==) operator
bool operator== (purchase& otherPurchase)
{
if(description == otherPurchase.description)
{
if(cost == otherPurchase.cost)
{
return true;
}
else
return false;
}
else
return false;
}
//Less than(<) operator definition
bool operator< (purchase& otherPurchase)
{
return (description < otherPurchase.description);
}
//Overloading insertion(<<) operator
friend ostream& operator<< (ostream& os, const purchase& otherPurchase)
{
os << otherPurchase.description << " Cost: $" << otherPurchase.cost;
}
//Overloading extraction(>>) operator
friend istream& operator>> (istream& is, purchase& otherPurchase)
{
is >> otherPurchase.description;
is >> otherPurchase.cost;
}
};
struct person
{
string name;
float limit;
person *next;
listT<purchase> bought;
//Less than(<) operator definition
bool operator< (person& otherPerson)
{
return (name < otherPerson.name);
}
//Overloading insertion(<<) operator
friend ostream& operator<< (ostream& os, const person& otherPerson)
{
os << otherPerson.name << " Limit: $" << otherPerson.limit;
}
//Overloading extraction(>>) operator
friend istream& operator>> (istream& is, person& otherPerson)
{
is >> otherPerson.name;
is >> otherPerson.limit;
}
};
and just in case, here is the rest of the code (sorry i know i shouldnt post it all but i really am not sure where the problem lies)
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include "listT.h"
using namespace std;
struct purchase
{
string description;
float cost;
//Overloading equality(==) operator
bool operator== (purchase& otherPurchase)
{
if(description == otherPurchase.description)
{
if(cost == otherPurchase.cost)
{
return true;
}
else
return false;
}
else
return false;
}
//Less than(<) operator definition
bool operator< (purchase& otherPurchase)
{
return (description < otherPurchase.description);
}
//Overloading insertion(<<) operator
friend ostream& operator<< (ostream& os, const purchase& otherPurchase)
{
os << otherPurchase.description << " Cost: $" << otherPurchase.cost;
}
//Overloading extraction(>>) operator
friend istream& operator>> (istream& is, purchase& otherPurchase)
{
is >> otherPurchase.description;
is >> otherPurchase.cost;
}
};
struct person
{
string name;
float limit;
person *next;
listT<purchase> bought;
//Less than(<) operator definition
bool operator< (person& otherPerson)
{
return (name < otherPerson.name);
}
//Overloading insertion(<<) operator
friend ostream& operator<< (ostream& os, const person& otherPerson)
{
os << otherPerson.name << " Limit: $" << otherPerson.limit;
}
//Overloading extraction(>>) operator
friend istream& operator>> (istream& is, person& otherPerson)
{
is >> otherPerson.name;
is >> otherPerson.limit;
}
};
int main()
{
int nodeNumber = 0, count = 0, choice, nTotal, lTotal;
person *temp, *people = NULL, *current, *previous = NULL, *last = NULL, personData;
ifstream inFile;
string selection;
float total, tTotal, dTotal, difference, price;
purchase item;
//Open file and check for success
inFile.open("people.data");
if(inFile.fail())
{
cout << "Error opening file, program terminated.";
exit(1);
}
inFile >> personData;
while(inFile)
{
current = people;
temp = new person;
temp->name = personData.name;
temp->limit = personData.limit;
temp->next = NULL;
if(people==NULL)
{
people = temp;
}
else
{
while(current!=NULL && current->name < temp->name)
{
previous = current;
current = current->next;
}
if(current==NULL)
{
previous->next = temp;
}
else if(previous==NULL)
{
temp->next = people;
people = temp;
}
else
{
previous->next = temp;
temp->next = current;
}
}
nodeNumber++;
delete current;
inFile >> personData;
}
inFile.close();
inFile.clear();
//Open file and check for success
inFile.open("purchases.data");
if(inFile.fail())
{
cout << "Error opening file, program terminated.";
exit(1);
}
count = 0;
current = people;
inFile >> selection;
inFile >> item;
while(inFile)
{
while(count < nodeNumber)
{
if(current->name == selection)
{
if(current->bought.isFull())
{
cout << "List is Full.\n";
break;
}
if(current->bought.seqSearch(item) == -1)
{
current->bought.insert(item);
}
else
cout << item.description << " is already in the list.\n";
}
current = current->next;
count++;
}
inFile >> selection;
inFile >> item;
}
inFile.close();
inFile.clear();
do
{
cout << "\n1) Display the people on our Christmas list." << endl;
cout << "2) Display the people who we still need to shop for." << endl;
cout << "3) Display the purchases for a certain person." << endl;
cout << "4) Add a new person to the list." << endl;
cout << "5) Add a purchase for a certain person." << endl;
cout << "6) Delete a person from the list." << endl;
cout << "7) Quit." << endl;
cout << "Please enter your choice: ";
cin >> choice;
switch(choice)
{
case 1: //Display a list of people on our Christmas list
count = 0;
current = people;
cout << endl;
while(count < nodeNumber)
{
cout << current->name << endl;
current = current->next;
count++;
}
break;
case 2: //Display a list of people who we still need to shop for
cout << fixed << showpoint << setprecision(2) << left;
count = 0;
nTotal = 0;
tTotal = 0;
lTotal = 0;
dTotal = 0;
current = people;
cout << endl;
cout << "Name Total Limit Difference" << endl;
cout << "-------------------------------------------" << endl;
while(count < nodeNumber)
{
total = 0;
for(int i = 0; i < current->bought.listSize(); i++)
{
current->bought.retrieveAt(i, item);
total += item.cost;
if(i == (current->bought.listSize() - 1))
{
if(total < current->limit)
{
difference = current->limit - total;
cout << setw(10) << current->name
<< setw(10) << "$" << total
<< setw(10) << "$" << current->limit
<< setw(10) << "$" << difference
<< endl;
nTotal++;
tTotal += total;
lTotal += current->limit;
dTotal += difference;
}
}
}
current = current->next;
count++;
}
cout << endl << setw(10) << nTotal << setw(10) << tTotal
<< setw(10) << lTotal << setw(10) << dTotal << endl;
break;
case 3: //Display a list of purchases for a certain person
cout << "\nPlease enter a person's name from the list: ";
cin >> selection;
cout << endl;
count = 0;
current = people;
while(count < nodeNumber)
{
if(current->name == selection)
{
cout << current->name << endl << "Spending limit: $" << current->limit
<< endl << "Purchases"
<< endl << "---------";
for(int i = 0; i < current->bought.listSize(); i++)
{
current->bought.retrieveAt(i, item);
cout << item.description << " $" << item.cost << endl;
}
}
current = current->next;
count++;
}
break;
case 4: //Add a new person to the list
cout << "\nPlease enter a new person's name and spending limit: ";
cin >> personData;
current = people;
temp = new person;
temp->name = personData.name;
temp->limit = personData.limit;
temp->next = NULL;
if(people==NULL)
{
people = temp;
}
else
{
while(current!=NULL && current->name < temp->name)
{
previous = current;
current = current->next;
}
if(current==NULL)
{
previous->next = temp;
}
else if(previous==NULL)
{
temp->next = people;
people = temp;
}
else
{
previous->next = temp;
temp->next = current;
}
}
nodeNumber++;
delete current;
break;
case 5: //Add a purchase for a certain person
cout << "\nPlease enter a person's name from the list: ";
cin >> selection;
cout << "Please enter the gift description in the first field then cost in the next: ";
cin >> item;
cout << endl;
count = 0;
current = people;
while(count < nodeNumber)
{
if(current->name == selection)
{
if(current->bought.isFull())
{
cout << "List is Full.\n";
break;
}
if(current->bought.seqSearch(item) == -1)
{
current->bought.insert(item);
}
else
cout << item.description << " is already in the list.\n";
}
current = current->next;
count++;
}
break;
case 6: //Delete a person from the list
bool found;
cout << "Please enter a name of someone to remove: ";
cin >> selection;
if (people == NULL)
cout << "Cannot delete from an empty list." << endl;
else
{
current = people;
found = false;
while (current != NULL && !found)
if (current->name == selection)
found = true;
else
{
previous = current;
current = current->next;
}
if (current == NULL)
cout << "The item to be deleted is not in the list." << endl;
else
if (current->name == selection)
{
if (people == current)
{
people = people->next;
if (people == NULL)
last = NULL;
delete current;
}
else
{
previous->next = current->next;
if (current == last)
last = previous;
delete current;
}
nodeNumber--;
}
else
cout << "The item to be deleted is not in the list." << endl;
}
break;
case 7: //Quit program
cout << "Goodbye.\n\n";
break;
default:
cout << "Invalid option please try again.\n\n";
break;
}
} while(choice != 7);
return 0;
}
It is due at the next hour, so in thirty minutes. someone could please help me with a quick fix that would be great. THANKS