Reading text file into a struct

Hello everyone!

I have a project due soon and I am really frying my brain. I have scoured the internet for some answers but I can't seem to get a straight one. I don't understand how to open a .txt and read the information into a struct and how to display that struct. I can probably figure out the rest of what I need to do for the program if I could only have someone help me figure this part out. The code I have so far is displayed below, I know it's more or less a skeleton. If someone could just hang out and give me hints while I develop the function on my own, that would be great!

here's a sample .txt
Goofy dog 20
Minnie mouse 15
Donald duck 22
ect...



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <iomanip>
#include <fstream>



using namespace std;

# define fLength 32
# define nLength 20
# define sLength 10
# define MAX 15
# define WIDTH 11

struct Animals
{
	char name[nLength];
	char species[sLength];
	int age;
};

void displayMenu();
void openFile(ifstream &inFile);

ifstream inFile;

void openFile(ifstream &inFile)
{
	char fileName[fLength];
	cout << "Please enter the filename: ";
	cin >> fileName;
	cout << "\n\n";
		
	inFile.open(fileName, fLength);

	if (!inFile)
	{
		cout << "The file '" << fileName << "' could not be opened.\n\n";
	}
}

void displayMenu()
{
	cout << "\nMenu:\n";
	cout << "-------\n";
	cout << "1 - Display all animals in forward order\n";
	cout << "2 - Display all animals in reverse order\n";
	cout << "3 - Display all animals sorted by age (youngest first)\n";
	cout << "4 - Determine average animal age\n";
	cout << "5 - Determine which animal is oldest\n";
	cout << "6 - Determine which animal is youngest\n";
	cout << "Q - Quit the program\n";
}

int main()
{
	openFile(inFile);

	while (inFile)
	{
	char choice [7];

	displayMenu();
	cout << "\nPlease enter your choice: ";
	cin >> choice;
	cout << "\n\n";

	if (choice[0] == '1')
	{
		
	}
	else if (choice[0] == '2')
	{

	}
	else if (choice[0] == '3')
	{

	}
	else if (choice[0] == '4')
	{

	}
	else if (choice[0] == '5')
	{

	}
	else if (choice[0] == '6')
	{

	}
	else if (choice[0] == 'q' || choice[0] == 'Q')
	{
		cout << "Goodbye!\n\n";
		system("pause");
		return 0;
	}
	else if (choice[0] < '1' || choice[0] > '6')
	{
		cout << "Sorry, but that's not a valid option.\n";
		cout << "Please choose again\n";
	}
	else if (choice[0] != 'q' || choice[0] != 'Q')
	{
		cout << "Sorry, but that's not a valid option.\n";
		cout << "Please choose again\n";
	}

	}


	system ("pause");
	return 0;
}
You need to declare an Animal array to hold the info from the file - let's stick to your example - you have 3 animals in file, so you need to declare
Animal animal[3];
Then, if your inFile is correctly opened, just read it in sequence. Whitespaces are used to split variables.
1
2
3
4
5
6
7
8
9
10
11
int i = 0;
while(inFile)
{
    inFile >> animal[i].name >> animal[i].species >> animal[i].age;
    i++;
}
// And see if this works:
for(int i = 0; i < 3; i++)
{
    cout << animal[i].name << animal[i].species << animal[i].age << endl;
}

Also, open file like this:
inFile.open(fileName, std::ios::in);
Well, to begin with, you need to be able to read just a single animal from the file. Here's one way to do that:
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
#include <iostream>
#include <fstream>

using namespace std;

const int nLength = 20;
const int sLength = 10;

struct Animals
{
    char name[nLength];
    char species[sLength];
    int  age;
};

int main()
{
    std::ifstream fin("input.txt");

    Animals a;
    
    fin >> a.name >> a.species >> a.age;
    
    cout << "Name: " << a.name << " Species: " << a.species << " Age: " << a.age << endl;
}


After that, you can develop the code to work with an array of Animals, something like this:
1
2
3
const int maxAnimals = 100;

Animals array[maxAnimals];



By the way, I don't think you need an array here, char choice [7];, a single character would be easier to work with, char choice; and the menu selection might be done better using switch/case,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    cin >> choice;
    cout << "\n\n";

    switch (choice)
    {
        case '1':
            break;
        
        case '2':
            break;
        
        case '3':
            break;
        
        // etc.

        default:
            cout << "Sorry, but that's not a valid option.\n";
            cout << "Please choose again\n";
    }
Hello!
How about this:
http://www.java2s.com/Tutorial/Cpp/0160__Structure/Usecintoreaddataforastructure.htm

What would mean:
struc student stu[4] ?

Is that sth like an array (name= stu) JOINED to to structure student (name of the structure = student)?

MANY THANKS!!!
Hello!
Please, what is "Animals a " in this example?

Judging after "." I suppose it is a structure, joining somhow to structure "struct Animals" from above.

is that right?

many thanks!
What would mean:
struc student stu[4] ?

Is that sth like an array (name= stu) JOINED to to structure student (name of the structure = student)?

I don't know what you mean by "JOINED to". It's an array called stu of 4 elements of type struct student. In other words, it's an array of 4 structures.

Please, what is "Animals a " in this example?


It's declaring a single object called a of type Animals. Animals is defined as a structure at line 9, so it's declaring a single structure of that type.
ok so after messing around with it for a bit....I think I've gained some progress, although not much. I still don't think I'm actually reading the information into the file:

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

# define fLength 32
# define nLength 20
# define sLength 10
# define MAX 15
# define WIDTH 11
# define MAX_NUM_ANIMALS 15

struct Animals
{
	char name[nLength];
	char species[sLength];
	int age;
};

void showArray (char name, char species, int age, int i);
void displayMenu();
void openFile(ifstream &inFile);
void loadFile(Animals arrayOfCritters[MAX_NUM_ANIMALS], int animalCount);

ifstream inFile;
char name[nLength];
char species[sLength];
int age;
int animalCount;

void loadFile(Animals arrayOfCritters[MAX_NUM_ANIMALS], int animalCount)
{
	struct Animals critters[MAX_NUM_ANIMALS];
	while (!inFile.eof());
	{
		inFile >> critters[animalCount].name >> critters[animalCount].species >> critters[animalCount].age;
               animalCount++;
	}
	 for(animalCount = 0; animalCount <= 3; animalCount++)
        {
                cout << critters[animalCount].name << critters[animalCount].species << critters[animalCount].age;
        }
}

void openFile(ifstream &inFile)
{
	char fileName[fLength];
	cout << "Please enter the filename: ";
	cin >> fileName;
	cout << "\n\n";
		
	inFile.open(fileName, fLength);

	if (!inFile)
	{
		cout << "The file '" << fileName << "' could not be opened.\n\n";
	}
}

void displayMenu()
{
	cout << "\nMenu:\n";
	cout << "-------\n";
	cout << "1 - Display all animals in forward order\n";
	cout << "2 - Display all animals in reverse order\n";
	cout << "3 - Display all animals sorted by age (youngest first)\n";
	cout << "4 - Determine average animal age\n";
	cout << "5 - Determine which animal is oldest\n";
	cout << "6 - Determine which animal is youngest\n";
	cout << "Q - Quit the program\n";
}

int main()
{
	openFile(inFile);

	while (inFile)
	{
	Animals arrayOfCritters[MAX_NUM_ANIMALS];
	char choice [2];

	displayMenu();	
	cout << "\nPlease enter your choice: ";
	cin >> choice;
	cout << "\n\n";

	if (choice[0] == '1')
	{
		loadFile(arrayOfCritters, animalCount);
	}
	else if (choice[0] == '2')
	{

	}
	else if (choice[0] == '3')
	{

	}
	else if (choice[0] == '4')
	{

	}
	else if (choice[0] == '5')
	{

	}
	else if (choice[0] == '6')
	{

	}
	else if (choice[0] == 'q' || choice[0] == 'Q')
	{
		cout << "Goodbye!\n\n";
		system("pause");
		return 0;
	}
	else if (choice[0] < '1' || choice[0] > '6')
	{
		cout << "Sorry, but that's not a valid option.\n";
		cout << "Please choose again\n";
	}
	else if (choice[0] != 'q' || choice[0] != 'Q')
	{
		cout << "Sorry, but that's not a valid option.\n";
		cout << "Please choose again\n";
	}

	}


	system ("pause");
	return 0;
}
Last edited on
Well, there are a number of issues here. For one thing, the use of global variables and then passing those variables as parameters is a kind of neither here nor there approach. If you use global variables, there's no need to pass them between functions, as they are already available in the global namespace. But that's generally a practice to be discouraged, so I'd very strongly recommend not to use any global variables. Move them so they are defined where needed, in this case in the main() function.

There are multiple problems with the way the input file is handled.

Line 53:
 
    inFile.open(fileName, fLength);
should be
 
    inFile.open(fileName);

Function loadFile() has several issues. The parameter animalCount should be passed by reference, so that its new value can be passed back after the function ends.

The array arrayOfCritters is received as an input/output parameter, but it is never used. Instead a new local array critters is declared. Using eof() in a while condition is almost always wrong. Here it is made worse by the semicolon at the end of line 35, which means an infinite loop with no way to terminate.

In the body of the loop, animalCount is incremented each time an animal is read. But then on line 40 that valuable information is erased and reset to zero, and instead a value of 3 is assumed.

Back in function main() the use of while (inFile) doesn't make a great deal of sense, as the loop should execute until the user decides to quit.

Based upon the menu options, none of which mention reading the data from the file, it seems a better plan would be to read from the file at the start of main, before entering that loop.

Well, I'm sure that's a lot to take in. So here's the code with the issues I mentioned straightened out.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

# define fLength 32
# define nLength 20
# define sLength 10
# define MAX 15
# define WIDTH 11
# define MAX_NUM_ANIMALS 15

struct Animals
{
    char name[nLength];
    char species[sLength];
    int age;
};

void showArray (char name, char species, int age, int i);
void displayMenu();
void openFile(ifstream &inFile);
void loadFile(Animals arrayOfCritters[MAX_NUM_ANIMALS], int & animalCount, ifstream &inFile);

void loadFile(Animals arrayOfCritters[MAX_NUM_ANIMALS], int & animalCount, ifstream &inFile)
{
    while (inFile >> arrayOfCritters[animalCount].name >> arrayOfCritters[animalCount].species >> arrayOfCritters[animalCount].age)
    {
        animalCount++;
    }

    for (int i = 0; i < animalCount; i++)
    {
        cout << arrayOfCritters[i].name << arrayOfCritters[i].species << arrayOfCritters[i].age << endl;
    }
}

void openFile(ifstream &inFile)
{
    char fileName[fLength];
    cout << "Please enter the filename: ";
    cin >> fileName;
    cout << "\n\n";

    inFile.open(fileName);

    if (!inFile)
    {
        cout << "The file '" << fileName << "' could not be opened.\n\n";
    }
}

void displayMenu()
{
    cout << "\nMenu:\n";
    cout << "-------\n";
    cout << "1 - Display all animals in forward order\n";
    cout << "2 - Display all animals in reverse order\n";
    cout << "3 - Display all animals sorted by age (youngest first)\n";
    cout << "4 - Determine average animal age\n";
    cout << "5 - Determine which animal is oldest\n";
    cout << "6 - Determine which animal is youngest\n";
    cout << "Q - Quit the program\n";
}

int main()
{
    ifstream inFile;
    char name[nLength];
    char species[sLength];
    int age;
    int animalCount = 0;

    Animals arrayOfCritters[MAX_NUM_ANIMALS];
    char choice = ' ';

    openFile(inFile);
    loadFile(arrayOfCritters, animalCount, inFile);

    while (choice != 'Q' && choice != 'q')
    {

        displayMenu();
        cout << "\nPlease enter your choice: ";
        cin >> choice;
        cout << "\n\n";

        if (choice == '1')
        {

        }
        else if (choice == '2')
        {

        }
        else if (choice == '3')
        {

        }
        else if (choice == '4')
        {

        }
        else if (choice == '5')
        {

        }
        else if (choice == '6')
        {

        }
        else if (choice == 'q' || choice == 'Q')
        {
            cout << "Goodbye!\n\n";
            system("pause");
            return 0;
        }
        else if (choice < '1' || choice > '6')
        {
            cout << "Sorry, but that's not a valid option.\n";
            cout << "Please choose again\n";
        }

    }

    return 0;
}

Last edited on
Topic archived. No new replies allowed.