Need help reading a file?

Hi everyone, I am assigned to create a program that reads from a list of people. The list is in the following order:

last name, first name SSN age street address

For example:

Brady,Marcia 000000131 17 4222 Clinton Way, Los Angeles, CA
My first job is to read the program and store all of the people in the list in an array. Now I can do that easily line by line but I am required to split everything up into variables: First Name, Last Name, SSN, age, street address.

What I originally did was used a for loop that looked like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
        for (int i = 0; i < count; i++) {
            
            //This line is used to pull data from the inputFile until it sees a comma, then stores it in the string variable "lastName".
            getline(inputFile, lastName[i], ',');

            //This line is used to pull data from the inputFile until it sees a space, then stores it in the string variable "firstName"
            getline(inputFile, firstName[i], ' ');

            //This line is used to pull data from the inputFile until it sees a space, then stores it in the string variable "SSN"
            inputFile >> SSN[i];
            
            //This line is used to pull data from the inputFile until it sees a space, then stores it in the string variable "age"
            inputFile >> age[i];
            
            //This line is used to pull data from the inputFile until it sees a space, then stores it in the string variable "address"
            getline(inputFile, address[i]);

            //Prints the information to the Terminal.
            cout << lastName[i] << "," << firstName[i] << " " << SSN[i] << " " << age[i] << " " << address[i] << "\n";

        }

And it worked, but I have a new issue. I am technically not supposed to know how many people would be in this list. Which means I need to use a while loop.

After this I need to be able to add new people to the list, search for people on the list by last name, and even be able to print out who was searched and found after all the searching is completed. Now this is quite a bit of work for the first homework assignement seeing as I have no experience with c++, but I do know a little bit of c.

Can anyone help me out?

Thanks!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    if (inputfile.is_open())
		{
		while ( inputfile.good())
			{
			// code here, example below
			getline (inputfile,line);

			}
		}

	else
	{
	        cout << "File not open" << endl;
	}
There's a small problem with SamuelAdams' code. The inputfile stream might not enter a bad state until it tries to read from the file after the last line. So, after reading the last line of text, inputfile.good() will still return true. It will not be false until after the next getline. To solve this, change while ( inputfile.good()) to while (true) and test for inputfile.good() after the getline(inputFile, lastName[i], ','); line, and break if the stream is not good.

Another better possibility is to put the first getline inside the while condition. When used like this, getline acts like a boolean and returns the state of the file stream after the read, which is what you want.

1
2
3
4
while(getline(inputFile, lastName[i], ','))
{
    // read rest of line and process data
}


The other problem with your code is the use of the index i. I'm assuming you are using a vector<string> or a string[]to store the data that has been resized ahead of time. If you use vector<string>, then you can use the push_back() method to add another piece of data. In this case, you will want to declare a string to read the data from the file and add to the vector as a separate line.

1
2
3
4
5
6
7
8
9
10
11
string data;

while(getline(inputFile, data, ','))
{
    lastName.push_back(data);

    getline(inputFile, data);
    firstName.push_back(data);

    // etc.
}


In each getline() call, data is overwritten with the new text from the file.
Topic archived. No new replies allowed.