help with making lines of a file its own array

This what i have so far im not sure exactly how to make the getline send the line to the array . i dont want to display the array just be able to search the arrays for information

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void loadarray()
{
        string studentarray[index++];
	string line;
	ifstream myfile("E:\\studentidarray.txt");
	if (myfile.is_open())
	{
		while (!myfile.eof())
		{
			getline(myfile, line);
			int index = 0;
			while (!myfile.eof())
			{
				getline(myfile, line);
			        cin >>  line >> studentarray[index++];
			}
		}
		myfile.close();
	}
	else cout << "Unable to open file";
}
Last edited on
studentarray[some_index] = line;

cin is for taking information from the keyboard. It has no place here.


Line 3 makes no sense at all.
Last edited on
that fixed it but i still got an issue. but when i run the search and enter a correct student id thats in file the display works correctly only for the last student id entered not any that have been entered. if that makes more sense.

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

void loadarray()
{
	
	string line;
	ifstream myfile("E:\\studentidarray.txt");
	if (myfile.is_open())
	{
		while (!myfile.eof())
		{
			getline(myfile, line);
			int index = 0;
			while (!myfile.eof())
			{
				getline(myfile, line);
				string studentarray[] = { line };
			}
		}
		myfile.close();
	}
	else cout << "Unable to open file";
}

void search_data()
{
	match = 'N';
	row = 0;
	maxsize = 8;

	cout << " enter a student id to be searched:    ";
	cin >> students;


	while (match == 'N' && row < maxsize)
	{
		if (students == studentid)      // studentid is input from user and is double
		{
			match = 'Y';
		}
		else
		{
			row = row + 1;
		}

		if (match == 'Y')
		{
			sucessful();
		}
		else
		{
			unsucessful();
		}
	}

	cout << " Another Search? ";
	cin >> moresearch;
	moresearch = toupper(moresearch);

	while (moresearch != 'Y' && moresearch != 'N')
	{
		cout << " Error ";
		cout << " Please enter Y or N as a response ";
		cin >> moresearch;
		moresearch = toupper(moresearch);
	}
	if (moresearch == 'Y')
	{
		search_data();
	}
}

void sucessful()
{
	cout << "student was found!" << endl;
	if (gpa >= 3.65)
	{
		cout << firstname << " " << lastname << " " << "is on the Dean's List." << endl;
	}
	if (gpa >= 2.0 || gpa < 3.65)
	{
		cout << firstname << " " << lastname << " " << "is an average student." << endl;
	}
	if (gpa < 2.0)
	{
		cout << firstname << " " << lastname << " " << "is on Academic Probation." << endl;
	}


}
void unsucessful()
{
	cout << "Student ID was not found" << endl;
	;
}
Last edited on
1
2
3
4
5
			while (!myfile.eof())
			{
				getline(myfile, line);
				string studentarray[] = { line };
			}


Every time you go round this loop, you are creating a new array of strings named studentarray, putting something in it, and then when the loop ends and you go round again, destroying it.

Create the array of strings once, before the loop. Add to it each time you go round the loop. Also, don't use an array. Use a vector<string>
Topic archived. No new replies allowed.