Need help for code. I'm stuck

I couldn't read the file from a text completely. Can someone help me out?
My code is like this:

Performer* readPerfData(string filename, int &n)
{
cout << "Read Performers Data...\n";
ifstream inFile;
inFile.open("performers.txt");
if (inFile.fail())
{
cout << "Unable to open file: "<< filename << endl; // show message if the file is not available
return NULL;
}
inFile >> n; // read the number of student
Performer *stu = new Performer[n]; // allocate new array with n size
for (int i = 0 ; i< n ; i++)
{
getline(inFile,stu[i].name);
inFile.ignore(0,';');
for (int j =0; j<5; j++)
{
inFile >> stu[i].scores[j];
}
}
inFile.close();
return stu;
}

the performers.txt is like this:

performers.txt
8
John Lee; 7.0 7.8 7.1 7.9 7.5
David T. Ng; 8.3 2.9 9.8 9.2 9.7
Mary Johnson; 8.3 8.0 8.9 7.9 8.5
Andy V. Garcia; 9.1 8.9 9.0 8.7 9.1
Ann Peterson; 9.0 8.8 9.1 9.7 9.3
Lucy A. Smith; 8.2 8.4 8.9 9.3 8.5
Dan Nguyen; 9.0 5.6 8.9 9.9 7.3
Sue K. Warren; 7.9 8.2 7.6 8.1 8.0

And the output is:
Sue K. Warren; 7.9 8.2 7.6 8.1 8.0
1
2
getline(inFile,stu[i].name);
inFile.ignore(0,';');

Just use ';' as the third parameter to getline
http://www.cplusplus.com/reference/string/string/getline/


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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

constexpr int PerformerNumScores = 5;
struct Performer {
	std::string name;
	double scores[PerformerNumScores];
		
	friend std::ostream& operator<<(std::ostream& os, const Performer& performer)
	{
		os << performer.name << ';';
		for (int i = 0; i < PerformerNumScores; i++)
		{
			os << ' ' << performer.scores[i];
		}
		return os;
	}
};

std::vector<Performer> read(const std::string& filename)
{
	std::ifstream fin(filename);
	if (!fin)
		return std::vector<Performer>();
	
	int n;
	if (!(fin >> n))
		return std::vector<Performer>();
	
	std::vector<Performer> performers(n);
	
	for (int i = 0; i < n; i++)
	{
		std::string name;
		getline(fin, name, ';'); // read until ;
		performers[i].name = name;
		
		for (int j = 0; j < PerformerNumScores; j++)
		{
			fin >> performers[i].scores[j];
		}
	}
	
	return performers;
}

int main()
{
	std::vector<Performer> performers = read("performers.txt");
	for (size_t i = 0; i < performers.size(); i++)
	{
		std::cout << performers[i] << '\n';
	}
}
Last edited on
@Ganado, You need to eat the newlines left after fin >> n and the fin >> ... loop. And note that you could just return {} for your error return.
Ah you're right, it's currently double-spacing the output but luckily doesn't affect the numbers. Thanks for the return tip. Btw welcome back after the hiatus.

Fixed with:
1
2
3
		std::string name;
		getline(fin >> std::ws, name, ';'); // read until ;
		performers[i].name = name;

Last edited on
This is what I got so far:
if (inFile.is_open())
{
inFile >> n; // read the number of student
stu = new Performer[n]; // allocate new array with n size
for (int i = 0 ; i < n ; i++)
{
inFile.ignore();
getline(inFile, stu[i].name, ';');
for (int j =0; j<5; j++)
{
inFile >> stu[i].scores[j];
}
}
}
return stu;

But It still didn't read the scores.
if it did not read the scores its probably out of sync with the file.
don't try to read integers right now.
take that out, and read just a throw-away string. then print what you got to the console with cout and endl. see what the program is getting in your loops.
compare that to what is in the file. see where it is getting lost and try to fix it this way, then you can put the integers back once it is doing what is expected.

Topic archived. No new replies allowed.