Could someone help me?

I have an file in which a list of employees and salaries are listed for each year

ex:

Year: 2005
No. of Employees: 3
Employee ID Salary
123456 36000.00
123567 32000.00
123678 33000.00
Year: 2006
No. of Employees: 4
Employee ID Salary
133456 31000.00
133567 32000.00
133678 33000.00

How do I open all these data into arrays?

This is my code, and I don't think it is good since it only reads the lines and not the data#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
int counter;
string countLines[20];
ifstream indata;
indata.open("employee.txt");
for(counter=0;counter<=20;counter++)
{
getline(indata,countLines[counter]);
cout<<countLines[counter]<<endl;
}
indata.close();
return 0;
}

wasn't the answers you received in the thread you created on 4 December sufficient? If not, you should just continue with that thread instead of creating a new thread with the same problem.

I think that you shouldn't be that mean. I am just trying to learn, and if whatever they wrote back to me doesn't work, I need to keep trying don't you think.

>I think that you shouldn't be that mean.
There was nothing mean about his post. It was politely worded as far as I can tell. The problem is that when you create a new thread for the same problem as your other thread, we basically lose all of the work that was done in the previous thread. This wastes your time as we have to start the troubleshooting process all over again to get a feel for your problem.

here is your old thread, as a reference. What did you try and what went wrong with it?

Member Avatar for iamthwee
No. of Employees: 4
Employee ID Salary
133456 31000.00  line 1
133567 32000.00  line 2
133678 33000.00  line 3

Why are there only three lines? Shouldn't there be four?

>>it only reads the lines

Thats what getline() does. It places all information in the input stream up to the indicated terminating character into the string indicated. If it the end of file indicator, EOF, before it finds a terminating char it will also cease input into the string indicated. Since the default deliminting value is the newline char, if there is no newline char in the file, then it will read in the whole file into a single string. In this case, there is probably a newline char at the end of each line. Therefore each line will be read into a "different" string. If you change the terminating char to be a colon char like this:

string temp;
getline(indata, temp, ':');

then you could read in the initial line of the file up to the colon after Year, remove the colon from the input stream, and just don't do anything with temp if you want. Then you could use a call to the >> operator to read the year into an int variable for later use if you want. You can break up (parse) the entire file into strings and numerical variables using getline() to read in strings and the >> operator to read in the numerical values, storing what you want in appropriate variables/arrays/whatever.

Be aware however that >> doesn't remove terminating whitespace char from the input stream. For instance it won't remove the newline char that's present in the file after 2006, even though you don't see a newline char when reading the file with your eyes. Therefore if you next call getline() with the default newline char as the terminating char the first thing getline() will find in the input stream is the newline char left behind by >> and it won't put anything useful in the string indicated. Then if you try reading 3 into a int variable the program will likely crash, because the string "No. of Employess:" is still in the input stream and you can't put a string into an int. To prevent this you should call the ignore() istream method after the call to >> if you are going to use a call to getline() next. Since you are reading in a file, it's unlikely there is more than one new line at the end of each line, though if there are any "empty" lines in the file there may be two newlines in succession. If there aren't any empty lines in the file, and your post suggests there isn't, then ignoring just a single char after the last consecutive call to >> should work. Otherwise you are going to need a more sophisticated syntax.

To avoid the hassle of throwing in calls to ignore() it is perfectly acceptable to read in the file line by line as one string per line using nothing but getline() as you originally did. However, then you would need to parse each string using a stringstream or a function that you create yourself thereby creating strings you can ignore and numerical variables you want to store.

It's probably just as easy to parse the file as you read it since it is predictable, but the choice is yours.

>>Why are there only three lines? Shouldn't there be four

Probably a typo and should be 3 instead of 4 just like the second number in the employee ID numbers are probably all 2s instead of 3s so that all three employees are represented in 2006 and 2005 rather than 3 different employees in each year, but who knows for sure.

Could someone help me?

I have an file in which a list of employees and salaries are listed for each year

ex:

Year: 2005
No. of Employees: 3
Employee ID Salary
123456 36000.00
123567 32000.00
123678 33000.00
Year: 2006
No. of Employees: 4
Employee ID Salary
133456 31000.00
133567 32000.00
133678 33000.00

How do I open all these data into arrays?

This is my code, and I don't think it is good since it only reads the lines and not the data

After you've read a line from the file as a string, you can parse the string to extract the data you need.

It would be helpful to define the rules as to the layout of your file (eg, first line is always a header containing the year, second line is always a header containing the number of employees, etc). At first glance its not immediately clear whether your file has any consistancy. If possible, you may want to change the format of your file somewhat (eg, by adding delimiting characters which show the start and end of the data, and even its type). Once you've clearly defined the format of your file, you can create functions which will parse the input to extract each item of useful data, and ignore superfluous information.

When you've defined your file format 'rules' - try creating a function which parses just the first line, and test it. If you can manage to do that, the rest of the file shouldn't be too different.

(Hint : When it comes to converting numbers to integral or floating point types, use std::stringstream)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.