Reading a mixed data type and taking certain data and placed in an array

Ok, so I'm given a .txt file that is in this format:

John
32
0
1
James
12
2
1
Kevin
3
1
0

I don't know exactly how many of these sets there will be. Now, I have to basically pull out 4 lines at a time until eof. How am I going to be able to do that after opening and reading a file? Also, How will I take the 2nd line of each data set (the second line being 32, 12, 3, .... etc.) and put them into an array? Please help, I'm beyond confused.
Can anyone help me out?
It looks here like you're storing information about a person, which can be represented as an object in your code. Could you elaborate more on what you're trying to do so I can get a more clear picture?
So I have a prototype that reads: void read_data(ifstream& input, int arr[]);

Now, in that read function he wants us to read the mixed data type (using getline() and "cin >>" statements) and opening and reading the .txt file until the end of the file.

Then, he wants us to take only the second line for each person, so (32, 12, 3) except its possible it will be for more people. He wants us to take the second lines and put it into an array.

Thanks for helping
Really confused on this and I'm stumped. Anyone?
Since you have different data types, you can't really use a single built-in type. Either you'll have to use one array of strings (or a vector, or other container) for the names and one for the numbers, or 3 for the numbers (one for each in a set). A better option would be, like Thumper suggested, a user-defined type. In other words, a structure in this case (since you don't really need member functions at this point.) By using a structure you can read each line into the structure, then create a new structure and read the next 4 lines into it, etc etc, without having to bounce all around the input file.

As for reading each line you can use either the >> operator, which will read to but discard ANY white space (including new line characters) or the getline() function which will read an entire line to the newline, but leave the newline in the stream which you will then have to discard manually.
Last edited on
Well, my instructor has said that we are not allowed to use structures for this assignment.
I also have to pass the array as a parameter from the array that had only read the 2nd line.

This is what I have so far, and I honestly don't know where I'm going with it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>
using namespace std;

void readData(ifstream& input, int length[]);

int main()
{
	std::vector<int> array;

	std::ifstream file ("schedule.txt");
	int n;
	while (file >> n) array.push_back(n);

	array.size();

	return 0;
}

void readData(ifstream& input, int length[])
{
	ifstream schedule;
	schedule.open ("schedule.txt", ifstream::in);

	if (!schedule.is_open())
	{
		exit(1);
	}

	string line;

	while (!schedule.eof())
	{
		getline(schedule, line);
	}

	schedule.close();
}
Your array is declared as a vector. Naming a vector type with the "array" name is a bit confusing, especially later on when you actually use it.

Also, like I said before, you can't store the name and the numbers in the same array, so you are going to need more then 1 container. Assuming that each of the numbers within a set of 4 data you are reading each stand for something different, you shouldn't really use a single array just for numbers either, though you can it would require a lot more work to code.

It is required that you use a function to read the input?

Edit: Actually, you can store all the info in one array, but you'll have to cast each character in the name to an int to do so. This is far more complex though since you'll have to have some way to remember how many ints make up each name, then cast them back to char to output them.
Last edited on
Topic archived. No new replies allowed.