Well, I did some debugging and figured out most of my problems, but when I run it with the second set of input data, the output makes no sense at all. When I ran it through the debugger with this input, it told me the segmentation fault occured at line 256. I would greatly appreciate any help that can be offered with for this problem. Here is my updated code and the output when I run it with the second input data (line 256 is in bold):
Code:
#include <iostream>
#include <map>
#include <vector>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
int main() {
ifstream infile("test.txt");
double num;
int location;
vector<string> people;
map<string,double> ower, owee, calculated;
string line;
string number;
string person;
// Get all the info out of each line
while( getline(infile,line) ) {
if (line.empty()) {
map<string,double>::iterator itOwee, itOwer;
// stage 1
for (itOwer = ower.begin(); itOwer != ower.end(); itOwer++) {
for (itOwee = owee.begin(); itOwee != owee.end(); itOwee++) {
if (itOwer->second == itOwee->second) {
cout << itOwer->first << " pays " << itOwee->first << " " << itOwee->second << endl;
owee.erase(itOwee->first);
ower.erase(itOwer->first);
break;
}
}
}
// stage 2
for (itOwer = ower.begin(); itOwer != ower.end(); itOwer++) {
for (itOwee = owee.begin(); itOwee != owee.end(); itOwee++) {
if (itOwee->second > itOwer->second) {
double payment = itOwee->second - itOwer->second;
cout << itOwer->first << " pays " << itOwee->first << " " << payment << endl;
owee[itOwee->first] = (itOwee->second - payment);
ower.erase(itOwer->first);
}
else if (itOwee-> second == itOwee->second) {
cout << itOwer->first << " pays " << itOwee->first << " " << itOwee->second << endl;
owee.erase(itOwee->first);
ower.erase(itOwer->first);
break;
}
else {
cout << itOwer->second << " " << itOwee->second;
double payment = itOwer->second - itOwee->second;
cout << itOwer->first << " pays " << itOwee->first << " " << payment << endl;
owee.erase(itOwee->first);
}
}
}
}
else {
people.clear();
person = "";
string::size_type len = line.length();
// Check if line is positive or negative and get the info out of it
if (line[0] == ' ') {
number = line.substr(1,line.find(" ",1));
num = atoi(number.c_str());
for (int i = line.find(" ",1)+1; i < len; i++) {
if (line[i] != ' ') person += line[i];
else {
people.push_back(person);
person = "";
}
}
people.push_back(person);
}
else {
number = line.substr(0,line.find(" ",0));
num = atoi(number.c_str());
for (int i = line.find(" ",0)+1; i < len; i++) {
if (line[i] != ' ') person += line[i];
else {
people.push_back(person);
person = "";
}
}
people.push_back(person);
}
// if number is positive
if (num > 0) {
calculated.clear();
int totalPeeps = people.size(); // check the number of people in deal
int totalSubject = 1; // total number of current people
string subject = people[0]; // first person
for (int i = 1; i < totalPeeps; i++)
if (subject == people[i]) totalSubject++; // check if owes more than once
double amount = (num * totalSubject) / totalPeeps; // calculate amount owed
amount = num - amount; // since already paid full amount, subtract amount owed from amount paid
if (owee.count(subject) > 0) owee[subject] += ceil(amount);
else owee[subject] = ceil(amount); // put into owee map
calculated[subject] = 0; // put into calculated map
int i = 1;
// calculate rest of the people in the deal
for (i = 1; i < totalPeeps; i++) {
subject = people[i];
totalSubject = 1;
// check if already calculated
if (calculated.count(subject) == 0) {
// make sure not at the end
if (i != (totalPeeps - 1)) {
// check if person in deal more than once
for (int j = i+1; j < totalPeeps; j++)
if (subject == people[j]) totalSubject++;
amount = (num * totalSubject) / totalPeeps; // calculate amount owed
// chcek if ower in previous deal
if (ower.count(subject) == 0) ower[subject] = ceil(amount);
// increase amount if already owes something
else {
amount += ower[subject];
ower[subject] = ceil(amount);
}
calculated[subject] = 0; // put in calculated map
}
// at the end, no need to check other people
else {
amount = (num * totalSubject) / totalPeeps;
if (ower.count(subject) == 0) ower[subject] = ceil(amount);
else {
amount += ower[subject];
ower[subject] = ceil(amount);
}
calculated[subject] = 0;
}
}
}
}
// if number is negative
else {
calculated.clear();
int totalPeeps = people.size(); // get people involved in deal
int totalSubject; // total times subject in deal
num *= -1; // make number positive
string subject = people[0]; // get first person who is owed
double amount = num; // owed full amount
// check if person already owed something
if (ower.count(subject) > 0) {
// if so, subtract amount he owes from the amount he is owed
if (amount < ower[subject]) ower[subject] = ceil(ower[subject] - amount);
// if amount is more than he is owed, he becomes an ower
else {
owee[subject] = ceil(amount - ower[subject]);
ower.erase(subject);
}
}
else owee[subject] = ceil(amount); // put into owee map
calculated[subject] = 0; // put into calculated map
int i = 1;
// calculate rest in the deal
for (i = 1; i < totalPeeps; i++) {
subject = people[i];
totalSubject = 1;
// check if already calculated
if (calculated.count(subject) == 0) {
// see if at the end or not
if (i != (totalPeeps - 1)) {
// check if person in deal more than once
for (int j = i+1; j < totalPeeps; j++)
if (subject == people[j]) totalSubject++;
// calculate amount this person owes
amount = (num * totalSubject) / (totalPeeps - 1);
// check if person already owed something
if (owee.count(subject) > 0) {
// if so, subtract amount he owes from the amount he is owed
if (amount < owee[subject]) owee[subject] = ceil(owee[subject] - amount);
// if amount is more than he is owed, he becomes an ower
else {
ower[subject] = ceil(amount - owee[subject]);
owee.erase(subject);
}
}
else if (ower.count(subject) == 0) ower[subject] = ceil(amount);
else {
amount += ower[subject];
ower[subject] = ceil(amount);
}
calculated[subject] = 0;
}
// calculate person at end
else {
amount = (num * totalSubject) / (totalPeeps - 1);
if (owee.count(subject) > 0) {
if (amount < owee[subject]) owee[subject] = ceil(owee[subject] - amount);
else {
ower[subject] = ceil(amount - owee[subject]);
owee.erase(subject);
}
}
else if (ower.count(subject) == 0) ower[subject] = (amount);
else {
amount += ower[subject];
ower[subject] = ceil(amount);
}
calculated[subject] = 0;
}
}
}
}
}
}
map<string,double>::iterator itOwee, itOwer;
// stage 1
for (itOwer = ower.begin(); itOwer != ower.end(); itOwer++) {
for (itOwee = owee.begin(); itOwee != owee.end(); itOwee++) {
if (itOwer->second == itOwee->second) {
cout << itOwer->first << " pays " << itOwee->first << " " << itOwee->second << endl;
owee.erase(itOwee->first);
ower.erase(itOwer->first);
break;
}
}
}
// stage 2
for (itOwer = ower.begin(); itOwer != ower.end(); itOwer++) {
for (itOwee = owee.begin(); itOwee != owee.end(); itOwee++) {
if (itOwee->second > itOwer->second) {
double payment = itOwee->second - itOwer->second;
cout << itOwer->first << " pays " << itOwee->first << " " << payment << endl;
owee[itOwee->first] = (itOwee->second - payment);
ower.erase(itOwer->first);
}
else if (itOwee-> second == itOwee->second) {
cout << itOwer->first << " pays " << itOwee->first << " " << itOwee->second << endl;
owee.erase(itOwee->first);
ower.erase(itOwer->first);
break;
}
else {
cout << itOwer->second << " " << itOwee->second;
double payment = itOwer->second - itOwee->second;
cout << itOwer->first << " pays " << itOwee->first << " " << payment << endl;
owee.erase(itOwee->first);
}
}
}
system("pause");
return 0;
}
C pays A 6
CÁxÿÿÿÿ-6BÁÁÁ¨Á¸ÁÈÁØÁèÁøÂDeptDepDeptB C A Âp
ÿÿÿÿ 5 A B C A ¸ÂÐÂèÃÃÃ0ÃHã -4 A Dept
5 A B C A B
-6 B Dept
ÃhÃ`Á4ãã ã¨ã°ã¸ãÀãÈãÐãØãàãèãðãøäää Cåååååååååååæææææææææææçççççççççççèèèèèèèèèèè0éSeg mentation Fault