Cannot reading .csv

I have a program:

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <fstream>
#include <string>

#define MAX_SCORE 100

using namespace std;

struct Scoreboard
{
	string no, id, first_name, last_name, mid_term, end_term;
};

void readScore(string path_in, Scoreboard s[], size_t index)
{
	ifstream in;
	in.open(path_in);

	string No, ID, First_name, Last_name, Mid_term, End_term;

	if (!in)
	{
		cout << "Error!\n";
	}
	else
	{
		getline(in, No, ',');
		getline(in, ID, ',');
		getline(in, Last_name, ',');
		getline(in, First_name, ',');
		getline(in, Mid_term, ',');
		getline(in, End_term, '\n');
		while (getline(in, s[index].no, ','))
		{
			getline(in, s[index].id, ',');
			getline(in, s[index].last_name, ',');
			getline(in, s[index].first_name, ',');
			getline(in, s[index].mid_term, ',');
			getline(in, s[index++].end_term, '\n');
		}
		in.close();
	}	
}

void viewScore(Scoreboard s[], size_t index)
{
	cout << "No" << "			" << "ID" << "		" << "First name" << "		" << "Last name" << "			" << "Mid-term" << "		" << "End-term\n"; 
	for (int i = 0; i < index; i++)
	{
		cout << s[i].no << "		" << s[i].id << "		" << s[i].last_name << "		" << s[i].first_name << "		" << s[i].mid_term << "			" << s[i].end_term << endl;
	}
}

void editScore(string path, Scoreboard s[], size_t index)
{
	string stuid, Mid_term, End_term;

	cout << "Enter the student's ID you want to edit: ";
	cin >> stuid;

	ofstream out;
	out.open(path);

	if (!out)
	{
		cout << "Error!\n";
	}
	else
	{
		for (int i = 0; i < index; i++)
		{
			if (s[i].id == stuid)
			{
				cout << "1. Edit mid-term\n";
				cout << "2. Edit end-term\n";

				int choice;
				cout << "Choose(1 or 2): ";
				cin >> choice;

				switch (choice)
				{
				case 1:
					cout << "New mid-term: ";
					cin >> Mid_term;
					s[i].mid_term = Mid_term;
					break;
				case 2:
					cout << "New end-term: ";
					cin >> End_term;
					s[i].end_term = End_term;
					break;
				}
			}
		}

		out << "No,Student ID,Last name,First name,Mid-term,End-term\n";
		for (int i = 0; i < index; i++)
		{
			out << s[i].no << "," << s[i].id << ",";
			out << s[i].last_name << "," << s[i].first_name << ",";
			out << s[i].mid_term << "," << s[i].end_term << endl;
		}

		out.close();
	}
}

int main()
{
	const string inFileNames[]{ "E:/scoreboard1.csv", "E:/scoreboard2.csv", "E:/scoreboard3.csv", "E:/scoreboard4.csv", "E:/scoreboard5.csv" };
	size_t index1{}, index2{}, index3{}, index4{}, index5{};

	Scoreboard s1[MAX_SCORE], s2[MAX_SCORE], s3[MAX_SCORE], s4[MAX_SCORE], s5[MAX_SCORE];

	for (size_t i = 0; i < inFileNames->size(); i++)
	{
		switch (i)
		{
		case 0:

			readScore(inFileNames[i], s1, index1);
			break;
		case 1:
			readScore(inFileNames[i], s2, index2);
			break;
		case 2:
			readScore(inFileNames[i], s3, index3);
			break;
		case 3:
			readScore(inFileNames[i], s4, index4);
			break;
		case 4:
			readScore(inFileNames[i], s5, index5);
			break;
		}
	}

	cout << "1. View score\n";
	cout << "2. Edit score\n";
	int c;
	cout << "Choose: ";
	cin >> c;

	if (c == 1)
	{
		system("cls");
		cout << "1. Class 1\n" << "2. Class 2\n" << "3. Class 3\n" << "4. Class 4\n" << "5. Class 5\n";
		int d;
		cin >> d;
		system("cls");

		switch (d)
		{
		case 1:
			viewScore(s1, index1);
			break;
		case 2:
			viewScore(s2, index2);
			break;
		case 3:
			viewScore(s3, index3);
			break;
		case 4:
			viewScore(s4, index4);
			break;
		case 5:
			viewScore(s5, index5);
			break;
		}
	}
	else if (c == 2)
	{
		system("cls");
		cout << "1. Class 1\n" << "2. Class 2\n" << "3. Class 3\n" << "4. Class 4\n" << "5. Class 5\n";
		int d;
		cin >> d;
		system("cls");
		switch (d)
		{
		case 1:
			editScore(inFileNames[0], s1, index1);
			break;
		case 2:
			editScore(inFileNames[1], s2, index2);
			break;
		case 3:
			editScore(inFileNames[2], s3, index3);
			break;
		case 4:
			editScore(inFileNames[3], s4, index4);
			break;
		case 5:
			editScore(inFileNames[4], s5, index5);
			break;
		}
	}
	system("pause");
}

Here is one of the 5 .csv files:
1
2
3
4
No,Student ID,Last name,First name,Mid-term,End-term
1,18127222,Nguyen Xuan,Phuc,7.1,7.0
2,18127223,Mike,Pompeo,9.1,9.5
3,18127224,Nicolas,Maduro,6.0,7.0

When I compiled and choose the "View score", all I got on the console was only:
1
2
No       Student ID       Last name         First name       Mid-term         End-term
Press any key to continue...

What's wrong to the rest of the data? Haven't I already written command to output them?
On lin2 14: index is passed by value, hence the increment on line 39 will not have any effect outside of readScore(...). So either you pass it by reference or return the value.
Thank you, it works now.
How can you determine when a function should be passed by reference or value?
How can you determine when a function should be passed by reference or value?

You should really only pass Plain Old Data, like int, double, char, and trivial classes by value. Everything else should be passed by reference, const reference when ever possible. You should also avoid using "raw" arrays, prefer std::vector instead whenever possible. When you do use arrays then you should also pass the size of the array into your function.

For example
1
2
3
4
5

// This:
void readScore(string path_in, Scoreboard s[], size_t index)
// Should probably be:
void readScore(cont string& path_in, Scoreboard s[], size_t& num_scores) 


Notice the name change to a more descriptive name. The "index" purpose of this variable is really a secondary purpose, the outside world will be more interested in the "counting" feature of this variable. Also note that the std::string is passed as a const reference because a std::string is a non-trivial type.


You may use const to improve the security of your code like so:

void readScore(const string &path_in, const Scoreboard s[], const size_t index)

If you'd do this line 39 would raise a compiler error.
Got that, thank you!
Sorry for late reply!
Topic archived. No new replies allowed.