String Subscript out of range.

So I am trying to run this program, its not everything but I think the problem is in one of these functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void get_names(vector<string>& names, string name)
{
	ifstream inputfile;
	inputfile.open(name.c_str());  //open file
	if(!inputfile) throw 1;

	while(!inputfile.eof())
	{
		string inputs;

		inputfile >> inputs;
		names.push_back(inputs); //push back to vector of string

	}
	inputfile.close();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
void get_initials(vector<string>names, vector<char>& initials)
{
	for(int i=0; i<names.size();i++)
	{
		string buf;           //string to hold values of name
		stringstream in(names[i]); 
		in>>buf;

		initials.push_back(buf[0]); //get initials.

	}

}

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
int main()
try{

	char m;
	char l;
	char choice;
	string name; //name of file
	vector<string>names; //vector for names
	vector<char>initials; //vector for initials
	vector<int>chosen; //vector for names with initials
	
cout <<"Sooo, I'm going to need you to write the name of the file "
     <<"where you keep your names (include the .txt-- Like Names.txt "
     << "or something).\n"<<endl;
	cin >> name;
	
	cout << string(3,'\n');

	get_names(names,name);
	get_initials(names,initials);
	
	do{

	cout <<"OK, sooo what names do you want me to get for you? " << endl;
	cout <<"Names that begin with the letteeeer: ";
	cin >> l;
	//l=toupper(l);

	cout << string(2,'\n');

	get_chosen(initials,chosen,l);
	
	cout <<"Do you want them: "<<endl;
	cout <<"1)One by one" <<endl;
	cout <<"2)All together\n" <<endl;
	cin >> choice;
	cout << string(2,'\n');

	switch(choice)
	{
		case '1':
			if(chosen.size()==0){cout << "No names with that letter ." <<endl;}

			else

			cout <<"Juust keep pressing enter until you reach the end\n" <<endl;

			for(int i=0; i<chosen.size();i++)
			{
				cin.ignore();
				cout << names[chosen[i]];	
			}
			cout << string(3,'\n');
			break;

		case '2':
			if(chosen.size()==0){cout << "No names with that letter." <<endl;}

			else

			cout << "Here you go!\n" << endl;
			
			for(int i=0; i< chosen.size();++i){cout << names[chosen[i]] << string(1,'\n');}
			
			cout << string(3,'\n');

			break;
	
		default:
			cout <<"thaaat's not a choice" <<endl;
			break;
	}	

	
	cout <<"Do you want to look for another namee ooor "
		 <<"did you find what you were looking for?" <<endl;
	cout <<"y to repeat, anything else to end. " <<endl;

	cin >>m;

	if( m != 'y' && m !='Y')
	{
		string mesh;
		cout <<"\nOkidoky, this is it.\nGlad this program could help!. Later! "<<endl;
		cout <<"Soooo, now press anything to quiiit" <<endl;
		cin >> mesh;
	}

}while(m =='y' || m =='Y');

}

catch(int i)
{
	if(i==1) {cout <<"Emm..., problem. Did you type the name right? if yes...\n"
				   <<"Check the file in the folder. Type anything: "; string x; cin>> x; cout << endl;}
}


I get the string Subscript out of range error. The weird thing is that I was running this perfectly, but then i must've done something since it started shooting out this error. Im using visual studio 10 just in case its relevant.
Last edited on
Post the compiler error please.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void get_names(vector<string>& names, string name)
{
	ifstream inputfile;
	inputfile.open(name.c_str());  //open file
	if(!inputfile) throw 1;

	while(!inputfile.eof())
	{
		string inputs;

		inputfile >> inputs;
		names.push_back(inputs); //push back to vector of string

	}
	inputfile.close();
}


Checking for eof is almost always the wrong thing to do. At some point, inputfile>>inputs fails because the end of file is reached. Only on the next evaluation of the control expression do you find this out.

This means the last input you push_back is a string with nothing in it, and for which using an operator subscript of any value is going to be out of bounds. In get_initials, you then access that string with a subscript operator.

You could reduce your function to:

1
2
3
4
5
6
7
8
void get_names(vector<string>& names, string name)
{
	ifstream inputfile(name.c_str()) ;
	string inputs ;

	while (inputfile >> inputs)
		names.push_back(inputs) ;
}


There is no need to explicitly close the file. ifstream's destructor takes care of that.
Topic archived. No new replies allowed.