Dynamic Array Allocation Program

I am trying to read from a file the names, id numbers, and 4 grades of 5 stduents. Here is the code:
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int GRADES = 4;
const int STUDENTS = 5;
struct StudentInfo
	{
		string studentName;
		int studentNum;
		double studentScores[GRADES];
		double avgScore;
	};



void main()
{
	StudentInfo *ptr = new StudentInfo[STUDENTS];


	ifstream fin;
	string studentName;
	
	fin.open("studentdata.txt");

	getline(fin, ptr[0].studentName);
	
		
			for(int x = 0; x < STUDENTS; x++)
			{
				fin >> ptr[x].studentNum;
			
				
				for(int index = 0; index < GRADES; index++)
				{
					fin >> ptr[x].studentScores[index];
				}
				

				getline(fin, ptr[x+1].studentName);
				
			
			}
		for(int count = 0; count < STUDENTS; count++)
				{
					cout << ptr[count].studentName << endl;
					cout << ptr[count].studentNum << endl;
					for(int countx = 0; countx < GRADES; countx++)
						cout << ptr[count].studentScores[countx] << endl;
				}
			}

Here is the txt file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Amy Adams
10111
97 86 78 95
Ben Barr
20222
89 81 73 87
Carla Carr
30333
79 71 63 77
Don Davis
40444
69 62 58 67
Edna Eaton
50555
63 51 62 48

When I run the program this is the output:
Amy Adams
10111
97
86
78
95

-842150451
-6.27744e+066
-6.27744e+066
-6.27744e+066
-6.27744e+066

-842150451
-6.27744e+066
-6.27744e+066
-6.27744e+066
-6.27744e+066

-842150451
-6.27744e+066
-6.27744e+066
-6.27744e+066
-6.27744e+066

-842150451
-6.27744e+066
-6.27744e+066
-6.27744e+066
-6.27744e+066
Press any key to continue . . .
As you can see the program is reading the first students information and outputting that fine, but the rest of the students have bad values for output. I'm guessing it's something to do with the pointer, but I really can't figure it out. Any help as to why it won't read all of the students info?
You are mixing formatted and unformatted input extraction without taking the proper precautions. You are also accessing outside the bounds of the array referenced by ptr.

If '\n' is encountered in the input stream by getline, getline will extract an empty string. With that in mind, can you figure out what is happening here?
Okay, I don't know why but I swear I tried this earlier and it didn't work and after I read what you said I tried it again and now it's working. All I did was insert an "fin.ignore();" statement right before the getline statement and I got the output I wanted.
By the way, what do you mean by, "You are mixing formatted and unformatted input extraction without taking the proper precautions. You are also accessing outside the bounds of the array referenced by ptr."
Thanks!
Is there any easy way to learn this dynamic array concept.
Yes, there is a very simple way. std::vector<> is very easy to use.
http://www.mochima.com/tutorials/vectors.html
Topic archived. No new replies allowed.