Solved

How to construct a Map table from a file?

Posted on 2000-03-30
15
207 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now