LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-08-2011, 09:00 AM   #1
ash_o
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Rep: Reputation: Disabled
Exclamation file handling problem in c++ (USACO)


I have a query in a coding problem from USACO.

The question and my code are given below.

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.>

_______________________





And here's my code to this:



#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
class person{
public: string name;
int acc, each_gift, noofgifts;
void gift_give()
{
each_gift= acc/noofgifts;
acc=acc%noofgifts;
}
};

ofstream fout ("gift1.out");
ifstream fin ("gift1.in");
int n,i;
string varname, recname;
getline(fin,n);
person man[n];

for(i=0; i<n; i++)
{
getline(fin,varname);
man[i].name=varname;
}
for(i=0;i<n;i++)
{
getline(fin,man[i].name);
getline(fin,man[i].acc);
getline(fin,man[i].noofgifts);

man[i].gift_give();

for(j=0;j<man[i].noofgifts; j++)
{
getline(fin,recname);

for(k=0;k<n;k++)
{
if(recname == man[k].name)
man[k].acc += man[i].each_gift;
}
}


}

for(i=0;i<n;i++)
{
fout<<man[i].name<<' '<<man[i].acc<<endl;
}


return 0;



}

Last edited by ash_o; 07-08-2011 at 05:28 PM.
 
Old 07-08-2011, 04:25 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
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;

}
 
1 members found this post helpful.
Old 07-08-2011, 04:34 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
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.

Last edited by dugan; 07-08-2011 at 05:04 PM.
 
Old 07-08-2011, 05:15 PM   #4
ash_o
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by dugan View Post
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!

link: http://ace.delos.com/usacogate
sign-in here and its a problem on section 1.1 .

Last edited by ash_o; 07-08-2011 at 05:21 PM.
 
Old 07-08-2011, 05:25 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by ash_o View Post
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!

link: http://ace.delos.com/usacogate
sign-in here and its a problem on section 1.1 .
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.
 
Old 07-08-2011, 05:27 PM   #6
ash_o
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by crabboy View Post
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....

Last edited by ash_o; 07-08-2011 at 05:35 PM.
 
Old 07-08-2011, 05:30 PM   #7
ash_o
LQ Newbie
 
Registered: Jul 2011
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by dugan View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in File Handling immadisetty Programming 5 02-03-2010 01:51 AM
File Handling in C++ jmandumpal Programming 6 09-04-2008 09:18 PM
A file handling problem. ASRaj Programming 3 03-17-2008 06:09 AM
Problem in file handling viveksnv Programming 2 02-28-2008 07:09 AM
File Handling In C/C++ vikasumit Programming 5 06-18-2006 11:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration