Solved

How to construct a Map table from a file?

Posted on 2000-03-30
15
238 Views
Last Modified: 2010-04-02
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?
0
Comment
Question by:vivitron
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 4
15 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2670977
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;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 2671001
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.
0
 

Author Comment

by:vivitron
ID: 2671244
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 2671373
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?)

0
 
LVL 22

Expert Comment

by:nietod
ID: 2671380
#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;
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 2671383
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2671443
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2676817
Are you out there?
0
 

Author Comment

by:vivitron
ID: 2681009
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2681148
>> 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.  
0
 

Author Comment

by:vivitron
ID: 2681630
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.
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2681790
That is what

     IntMap[x] = y; // you might have to reverse x and y.

is doing.  In your case

IntMap[ReadAccountIndex] = ReadAccountNumber;
0
 
LVL 22

Expert Comment

by:nietod
ID: 2695982
vivitron, what is happening with this question?  Has it been answered?
0
 

Author Comment

by:vivitron
ID: 2700690
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2700906
There have been some technical difficulties with the site.  Some "history" seems to have gotten lost
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question