Having a problem!

This is a discussion on Having a problem! within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having trouble getting a program I am working on to work. The problem I'm working on is ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Having a problem!

    Hello,

    I am having trouble getting a program I am working on to work. The problem I'm working on is located here:

    http://www.cs.dal.ca/apics/contest04/E/E.txt

    Here is my code:

    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) ) {
            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 {
                            double payment = itOwer->second - itOwee->second;
                            cout << itOwer->first << " pays " << itOwee->first << " " << payment << endl;
                            owee.erase(itOwee->first);
                        }
                    }
                }
                
        return 0;
    }
    When I run it with the first data set mentioned in the problem, I get the following output:

    C pays A 200
    E pays G 200
    D pays B 67
    F pays B 0
    Press any key to continue . . .
    Almost right, but not quite. When I try to run it with the second data set, the output screen does something really fast and then crashes. I haven't tried to run it with both data sets in one input file yet. So, can anyone out there figure out why:

    a) With the first data set, the final payment to B is not correct? and
    b) With the second data set why it goes berserk and then crashes?


    Thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Get yourself a debugger... and get debugging. If you haven't got one/can't get one, then do it the old fashioned way and cout out some messages at key points in your program to help you see what's going on. Good luck, and have fun.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    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
    Cx-6BDeptDepDeptB C A p
    5 A B C A ¸0H -4 A Dept
    5 A B C A B
    -6 B Dept
    h`4 C0Seg mentation Fault

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok, this is very bizarre. I've down some further testing and everything's being taken correctly out of each line of input. When I run the program with both data sets in the same file, I get results for the first data set. Then, it looks like results begin to show for the second data set, but then a line of strange characters come up, followed by the text file for some reason, then more strange characters, then a segmentation fault. Please, any help is greatly appreciated! Here is my updated code, with the line that gdb says is giving the segmentation fault in bold:

    Code:
    #include <iostream>
    #include <map>
    #include <vector>
    #include <fstream>
    #include <string>
    #include <cmath>
    
    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] = ceil(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);
                        }
                    }
                }
                cout << endl;
                ower.clear();
                owee.clear();
                calculated.clear();
                people.clear();
            }
            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] = ceil(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);
                }
            }
        }         
           
        return 0;
    }
    It must be something with the iterator, but I am completely stumped. It seems to produce the strange characters when the first line of input has a negative number or when a string with more than one character in it is in the input. Here's the results I get:

    C pays A 200
    E pays G 200
    D pays B 67
    F pays B 67

    C pays A 6
    CXepDehAA(8HXhxDeptA C EDeptA C E (@Xp ø 400 A C E C
    200 B D F
    -200 G E A

    -4 A Dept
    5 A B C A B
    -6 B Dept

    (08@HPX`hpx C0Segmentat ion Fault

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Haven't looked to deeply at the code to determine the cause of your problem, but I did notice a couple things I'd like to point out...

    Quote Originally Posted by Zildjian
    else if (itOwee-> second == itOwee->second) {
    cout << itOwer->first << " pays " << itOwee->first << " " << itOwee->second << endl;
    owee.erase(itOwee->first);
    ower.erase(itOwer->first);
    break;
    }
    The line highlighted above will always be true.

    Quote Originally Posted by Zildjian
    if (owee.count(subject) > 0) owee[subject] += ceil(amount);
    else owee[subject] = ceil(amount); // put into owee map
    I believe the above code can be replaced with a simple:

    Code:
    owee[subject] += ceil(amount);
    I don't think you need any of the extra logic dealing with inserting or updating values in the map... at least in the above case.

    As for your other problem, maybe you can attach the input file and also explain what you would expect the output to be for that given input.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    How about splitting the code up into manageable functions before you end up with a 1000 line main
    I just lost interest when I saw the size of that function
    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.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    that's beautiful...stick a few goto's in there too...
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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