ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
now my problem arises because in the input file, the order of people named is not same as the order in which their detAIls are specified. So if i create an array of objects of the class, and assign them to each person, how do i proceed because of this change in order?
I know the loop "getline(fin,man[i].name)" (see below) is wrong.
CAn someone help w.r.t. my code???
<the post is long, but i would appreciate if anyone could help me ASAP.>
For starters, what compiler are you using? I could not get your code to compile, none of your getline calls were compiling, perhaps your compiler was casting some of the types for you.
You really don't want your class inside your main method. It needs to be outside, ideally in it's own file with the implementation in a header file. Also, it is good practice to make your class variables private and have public methods to access the data. This allows you to change how the class is implemented without changing the interface to the class.
For the most part, I kept your loops intact, I was able to move the gift giving loop into the person class.
This was my quick stab at the problem:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class person
{
private:
string name;
std::vector<string> recipient_list;
int must_give;
int amount;
public:
person() { }
person( string n ) { name = n; amount = 0; must_give = 0; }
void AddRecipient( string s ) { recipient_list.push_back( s ); }
void addGiftAmount( int i ) { must_give = i; }
string getName() const { return name; }
void take_gift( int i ) { amount = amount + i; }
int getAmount() { return amount; }
void giveGifts(std::map<string,person>& mp)
{
int recip_total = recipient_list.size();
int each_gift = must_give/recip_total;
int remaining = must_give % recip_total;
amount = amount - must_give + remaining;
for (vector<string>::iterator it = recipient_list.begin();
it != recipient_list.end(); ++it)
{
person& p = mp[*it];
p.take_gift( each_gift );
}
}
};
int main()
{
ofstream fout ("gift1.out");
ifstream fin ("gift1.in",std::ios::in);
int n;
fin >> n;
std::map<string,person> peoplemap;
for(int i=0; i < n; i++)
{
string nameFromFile;
fin >> nameFromFile;
person newMan( nameFromFile );
peoplemap[nameFromFile] = newMan;
}
for(int i=0; i < n; i++)
{
string Name;
int giftTotal;
int number;
fin >> Name;
fin >> giftTotal >> number;
person& p = peoplemap[Name];
p.addGiftAmount( giftTotal );
string recipient_name;
for(int j=0;j<number; j++)
{
fin >> recipient_name;
p.AddRecipient( recipient_name );
}
p.giveGifts( peoplemap );
}
for (map<string,person>::const_iterator it = peoplemap.begin();
it != peoplemap.end(); ++it)
{
person p = it-> second;
fout << p.getName() << " " << p.getAmount() << endl;
}
return 0;
}
Uhm, isn't this a competitive programming contest? Are you sure it's ethical to a) post an entire question here and b) ask people to "help" with it?
I'm trying to find the contact information of the USACO organizers, so that I can send them a link to this thread.
Ummm no, this is a part of the lessons they give to "train" p[eople... its a proper coirse guide (their training gateway). i would have posted it on their discussion board,but its very inactive, and i am running short on time. But since i'm new here, i'll remove it if its offensive. Sorry people!
Ummm no, this is a part of the lessons they give to "train" p[eople... its a proper coirse guide (their training gateway). i would have posted it on their discussion board,but its very inactive, and i am running short on time. But since i'm new here, i'll remove it if its offensive. Sorry people!
That may be true, which is why I refrained from outright accusing you of dishonesty. However, you should not probably not post the entire question here. There's a reason why access to the question is restricted to people doing them.
For starters, what compiler are you using? I could not get your code to compile, none of your getline calls were compiling, perhaps your compiler was casting some of the types for you.
You really don't want your class inside your main method. It needs to be outside, ideally in it's own file with the implementation in a header file. Also, it is good practice to make your class variables private and have public methods to access the data. This allows you to change how the class is implemented without changing the interface to the class.
For the most part, I kept your loops intact, I was able to move the gift giving loop into the person class.
Thanks! I don't know anything about "map" used here. Will look it up!
But now its taking up more memory than allowed....
That may be true, which is why I refrained from outright accusing you of dishonesty. However, you should not probably not post the entire question here. There's a reason why access to the question is restricted to people doing them.
I'm sorry, my bad! I thought because its a free training gateway and anyone could sign in and see it, it was okay, as no competition was involved.
But i've taken note of it, and removed the question. I apologize again.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.