Need help getting text from file

So you will probably find my code cringy. I think I can't open the file to begin with. Using VC++ 2012, Windows 10. Currently getting vector subscript out of range error after entering all inputs, having the file does not seem to matter. while(file) is just bypassed. It does compile however. All suggestions to overall code are welcome.

There is supposed to be 3 files, in format id;id for every line.
1.course_id;coursename
2.student_id;course_id
3.student_id;student_name

Program takes names of these 3 files as cin, then the course name, and has to print names of all students registered in course.

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

using namespace std;


//for left and right column
struct dstring
{
	string id1;
	string id2;
} ;



vector<dstring> v(string filename)  
//extracts files from file to a vector with 2 elements per cell
{
	vector<dstring> v;
	ifstream ifile;
	ifile.open(filename.c_str());
	string std;
	string buffer1 = "", buffer2 = "";
	int c=0;
	while(ifile)
	{
	getline (ifile, std);
	unsigned int n=0, k=0;
	
	while(std[n] != ';' && n < std.size())
	  {
	  buffer1 += std[n];
	  n++;
	  }
	n++;
	while(getline (ifile,std) && n < std.size())
	  {
	   
		  buffer2 += std[n];
	   n++;
	  }
	c++;
	dstring d;
	d.id1 = buffer1;
	d.id2 = buffer2;
	v.push_back(d);

   }
	ifile.close();
	return v; 
}

string idfinderleft(string str, vector<dstring> v1) 
	//searches string in the left column, returns the right column value
{
	unsigned int n=0;
	do
	{
		
		if(v1[n].id1.c_str() == str.c_str())
		{
			return v1[n].id2;
			
		}
		n++;

	}while(v1[n].id1.c_str() != str.c_str() && n < v1.size());

}

string idfinderright(string str, vector<dstring> v1)
//reversed variant of idfinder
{

	unsigned int n=0;
	do
	{
		
		if(v1[n].id2.c_str() == str.c_str())
		{
			return v1[n].id1;

		}
		n++;
	}while(v1[n].id2.c_str() != str.c_str() && n < v1.size());

}

//given course id, returns a vector that has student ids who participate in course
vector<string> enrolled(string str, vector<dstring> v1)
{
	
	unsigned int n=0;
	vector<string> v;

	while(n < v1.size())
	{
		if(v1[n].id2 == str)
		{
		v.push_back(v1[n].id1);
		}
		n++;
	}

	return v;
}




int main()
{
	unsigned int counter = 0;
	string filename1, filename2, filename3, courseid, coursename;

	cin >> filename1;

	vector<dstring> v1 = v(filename1);
	
	cin >> filename2;
	
	vector<dstring> v2 = v(filename2);
	
	cin >> filename3;
	
	vector<dstring> v3 = v(filename3);

	cin >> coursename;
	

	courseid = idfinderright(coursename, v1);

	vector<string> ve = enrolled(courseid, v2);

	while(counter < ve.size())
	{
		cout << idfinderleft(ve[counter], v3);

		counter++;


	}









	return 0;
}
Last edited on
The following link tackles a similar file-reading exercise and might give you a few ideas:
http://www.cplusplus.com/forum/beginner/208524/#msg982358
Oh, I need to open the file...
I updated the code, but the error is still the same.
Line 27: Do not loop on (! stream.eof()) or (stream.good()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> operation as the condition in the while statement.
1
2
3
  while (stream >> var) // or while (getline(stream,var))
  {  //  Good operation
  }


Line 62,81: You can't compare two strings that way. c_str() returns a pointer. You're comparing two pointers. Simply compare the two std::strings.
 
  if (v1[n].id1 == str)


Line 69,87: ditto for !=.



If I use the

 
while(stream >>var)

then this already does the job of getline, then I would not have to have another getline inside the loop?

Edit: Well thanks, that solves all the problems. Program now works.
Last edited on
Topic archived. No new replies allowed.