Copying text file to struct array

Pages: 12
I see the concept.

Thank you very much guys! I finally got my program to work.

Seriously, one last question;

When I display the text file, there is a integer 5 in the text file, a return key, and then the age, gpa and name.

5
22 2.2 Bill Clinton
33 3.3 Phil Jackson
44 4.0 Mary A Johnson
25 2.5 Adam Smith
65 3.2 George W Bush

I do not want to display this "5", how do I bypass the first line?

If I use a for loop starting from int i = 1, it will skip the whole line with Bill Clinton
see my program, which is below your this line

A window pops up saying: "Unhandled exception at x61D735BA (msvcp120d.dll) in Project7(2).exe: 0xC0000005: Access violation writing location 0x00000000.
Your code looks very similar to mine, yet it still outputs the beginning 5 no?
How do I remove the initial 5 when displaying the text file?
the 5 was used to know lines. did i print it anywhere? try my code in your pc
Last edited on
repeated:
this worked fine in my pc
inf.txt:
5
22 2.2 Bill Clinton
33 3.3 Phil Jackson
44 4.0 Mary A Johnson
25 2.5 Adam Smith
65 3.2 George W Bush


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
#include <iostream>
#include <fstream>
using namespace std;

struct PERSON
{
	char Name[50];
	int Age;
	float Gpa;
};
 

int main ()
{
	PERSON *ptr;
	int lines;

	ifstream f("inf.txt"); //change to your file name
	f >> lines; // 5 to lines, need not print it
	ptr = new PERSON [lines];
	
	for (int i = 0; i < lines; i++)
	{		 
		f >> ptr[i].Age;
		f >> ptr[i].Gpa;
		f.getline(ptr[i].Name, 50, '\n');
	}
	
	for (int i = 0; i < lines; i++) //  following are printed in console window
	cout << "name: " << ptr[i].Name << " age: " <<ptr[i].Age << " gpa: " << ptr[i].Gpa << endl;	
	
return 0;
}

	
Last edited on
I'm very confused. Your code worked fine, I tried manipulating my code to replicate yours using functions yet the 5 still shows.

It seems like, you have to read the first line (f >> lines) and then immediately copy the data and display it.

I tried doing this with calling functions inside functions to replicate the transition but it doesn't work.

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
#include <ctime>
#include <iostream>
#include <fstream>
#include <array>
#include <iomanip>

using namespace std;

struct PERSON
{
	int age;
	float gpa;
	char name[20];
};
PERSON *p;

void ArraySize(string Fname, int &x);
void CopyData(string Fname, int size);
void DisplayData(int x);

int main()
{
	time_t a;
	time(&a);
	cout << ctime(&a) << endl;
	
	
	int sizeArray;
	
	ArraySize("two.txt", sizeArray); //determine dnynamic array size

	
	cout << "The size of the array is " << sizeArray << endl;


	DisplayData(sizeArray); //Display the data

	system("PAUSE");
	return 0;


}

void ArraySize(string Fname, int  &x)
{
	int n;
	fstream f;
	f.open(Fname, ios::in);
	f >> n;

	x = n;
	p = new PERSON[n]; //Declare new size for array PERSON p

	/*for (int i = 0; i < n; i++)		THIS IS YOUR CODE
	{
		f >> p[i].age;
		f >> p[i].gpa;
		f.getline(p[i].name, 20, '\n');
	}

	for (int i = 0; i < x; i++)
	{

		cout << p[i].age << "\t " << left << setw(2) << p[i].gpa << right << setw(2) << " " << p[i].name << endl;
	}
	*/ 
	CopyData(Fname, x);

	f.close();

}

void CopyData(string Fname, int size)
{
	ifstream f(Fname);

	for (int i = 0; i < size; i++)
	{
		f >> p[i].age; 
		f >> p[i].gpa;
		f.getline(p[i].name, 20, '\n');
	}

	
}



void DisplayData(int x)
{
	cout << endl;

	cout << "AGE\t GPA\t NAME" << endl;
	for (int i = 0; i < 22; i++)
	{
		cout << char(220);
	}
	cout << endl;
	cout << setfill(' ');
	cout << fixed << showpoint << setprecision(2);

	for (int i = 0; i < x; i++)
	{
	
		cout << p[i].age << "\t " << left << setw(2) << p[i].gpa << right << setw(2) << " " << p[i].name << endl;
	}
}


f.seekg(1); //use in line 76 of your last code to ignore first 1 digit
Last edited on
What does f.seekg(); do?

f.seekg(1); means ignore the very first char/int/float/string?
it sets the position from where you read. f.seekg(0) is starting position.
Topic archived. No new replies allowed.
Pages: 12