Link to home
Start Free TrialLog in
Avatar of vivitron
vivitron

asked on

How to construct a Map table from a file?

I have a text file that has Account numbers and Record numbers in the following format:

Acct# Record#
64391 0001
87521 0002
.
.
etc.

How do I read the data from the file using map<int, int> so I only need to read the file once and then how can the map data be display to a DOS window?
Avatar of nietod
nietod

We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?

I can give you this to get you started.

you can read from a file using an fstream object.   You need to include the file <fstream> and you since the fstream class (and map) is defined in the "std" namespace you want may want to place a "using namespace std;"  line at the top of the program.

You can read from a file called "c:\somefile.txt" using code like

#include <fstream>
#include <string>
using namespace std;

int main()
{
   fstream Fil("c:\\somefile.txt",ios_base::in);
   int x;
   string Line;
   Fil >> x; // read an integer.
   getline(Fil,Line); // read a whole line.
    return 0;
}
To add things to a map you can use operator [].  Like if you want to add 10, to a map with a key of -5 you woudl do

map<int,int> IntMap;

IntMap[-5] = 10; // add a key of -5 with a value of 10.

you can "enumerate" the items in map using iterators.  the begin() function returns an iterator for the first item in the map  (or the end iterator if the map is empty)  you can use operator * to get the item that the iterator refers to and you can use operator ++ to get the next item in the list, for example.

map<int,int> IntMap;
map<int,int>::iterator CurItr; // -> current item in map.
map<int,int>::iterator EndItr; // -> end of map.

// Add stuff to map.

CurItr = IntMap.begin();
EndItr = IntMap.end();
while (CurItr != EndItr)
{
  //    Do something with the current item.  
  Itr++; // Get next item.
};

Can you use that information to create the program?  Try it and post what you get done.
Avatar of vivitron

ASKER

FYI, I am not a student and IT IS a good idea to reject answers if they don't work which, none of answers in the past have done.  Nietod, you are kindly requested never to respond to my questions again.
exucuse me?  None of my answers have worked?!  I'm sorry.  Do you have any question IDs you can give me?

I can assure you that all the information I presented here is correct.  Is there somethig that you feel does not work?  (here or in any other quesiton?)

#include <map>
#include <string>
#include <fstream>
using namespace std;

int main()
{
   fstream Fil("c:\\somefile.txt",ios_base::in);
   int x;
   int y
   string Line;
   map<int,int> IntMap;

   getline(Fil,Line); // read a whole line.
   while (!Fil.eof())
   {
      Fil >> x >> y;
      IntMap[x] = y; // you might have to reverse x and y.
           // I don't know what is the key for what.
   }
   map<int,int>::iterator CurItr; // -> current item in map.
   map<int,int>::iterator EndItr; // -> end of map.

   CurItr = IntMap.begin();
   EndItr = IntMap.end();
   while (CurItr != EndItr)
   {
      cout << CurItr->first<<, ' ' << CurItr->second << endl;
      CurItr++; // Get next item.
   };

   return 0;
}
You should understand that EE has a policy against answering accademic   questions.  (It is part of the customer agreement.)  If we can't tell if a question is accademic we have to err on the conservative side.
I've been reviewing your question history.  I've only answered one of your questions before.  So while it may be completely accurate to say none of my answers have worked, it also might be a bit unfair.  I don't like to brag (much) but I didn't reach #1 status by posting answers that don't work.  

Furthermore, I did point out a legitimate problem with that question.  That may not have been the only problem however, and that gets us to the 2nd point.  Questions tend not to be completely answered in a single "post".  Often they require several posts by the client asking the question and the expert working on the question.  This is usually because some information needed to solve the problem is not in the original question or because there actually are multiple problems that crop up as the earlier problems are fixed.

For this reason I really do suggest that you try to work with an expert to reach a solution.  (This point by the way is made in the official EE help as well.)   If you reject an expert's answer in cases where it is not wrong (even if it was not yet sufficient to help you) it tends to cause problems in the system.  It also may earn you a bad reputation and may limit your ability to get good help in the future.  So I really suggest you do not reject an answer unless you absolutely know it is wrong, or if you have given the expert a reasonable opportunity to help and he/she has failed.
Are you out there?
Nietod,
You had recently tried answering my "friend ostream& operator<<" question.  I've asked other questions in the past but I've not kept a record of who has answered.

Your comment to in my "friend" question about rejecting an answer should be directed to the Experts Exchange.  Experts Exchange only gives me two grading options.  Accept, with four possible grades and close question or Reject the answer and reopen the question.  If an answer does not work I still want to get an answer to my question.  I don't want to close the question.  That's why I select the Reject option.  Blame Experts Exchange for not offering a third choice of continued help from this expert.

My experience is in C programming not C++.  I've been given the task of maintaining a C++ program so you may find I ask for specific answers to my C++ questions and I'll offer substantial points to try and get that specific answer.  Other times I'm writing my own C++ programs and these questions may sound/look like I'm a student, but I'm not a student.  So keep this in mind when you or someone else answers my questions.  To give as much detail as possible on the C++ syntax and where to place your answers.

As for this question I still need help.
>> I've asked other questions in the past
Only 4 of them and I participated in the one.

>> Experts Exchange only gives me two grading options.
Yes, but you don't have to grade immediately.  Ask the expert for more information or explain any doubts you have before you accept or reject an answer.  

>> these questions may sound/look like I'm a
>> student, but I'm not a student.  
I don't doubt it.  But if a question suggests that it is accademic, we are forced to assume it is, at least until eveidence to the contrary is provided.

>> So keep this
>> in mind when you or someone else
>> answers my questions.  
I'l try, but that's not really feasable.  You are 1 of 100,000 clients of EE and I am one of 10,000 experts.  The numbers are stacked against this.  Your best bet it to make clear that a question is not academic in nature.  (at least on topics that seem academic, like this.)

>> As for this question I still need help.
Can you be more specific?  I provided the code.  
I can read and display the contents of the Account.idx file using:

long ReadAccountNumber, ReadAccountIndex;

while( infile )
{
  infile >> ReadAccountNumber >> ReadAccountIndex;

  if( !infile.eof() )
    cout << ReadAccountNumber, ReadAccountIndex << endl;
    }
infile.close();

I'm would like to place the data read into a map<double, double> template so when an an Account number is entered the Account Index is returned.  The Index is used for a random file access later in the code.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
vivitron, what is happening with this question?  Has it been answered?
I tried your answer of 4-3-00 @ 4:47pm last week.  It worked so I accepted this answer last week.  I'll try again reaccepting the answer.
There have been some technical difficulties with the site.  Some "history" seems to have gotten lost