need help in the string vector

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
#include <iostream>
#include <vector>
using namespace std;
int main(int arg, char*args[])
{
istream&getline(istream&is,string str,char delimiter);
vector<string,allocator>*retrieve_text()
{
	string file_name;
	cout<<"please enter file name:";
	cin>>file_name;
	ifstream infile(file_name.c_str(), ios::in);
	if(!infile)
	{
		cerr<<"sorry,can't open the file"<<file_name<<endl;
		exit(-1);
	}
	else cout<<endl;
		vector<string,allocator>*line_of_text=new vector<string,allocator>;
	string textline;
	string texline;
	typedef pair<string::size_type,int>stats;
	stats maxline;
	int linenum=0;
	while(getline(infile,textline,'\n';))
	{
		cout<<"line read:"<<textline.size())
		if(maxline.first<<textline.size())
		{
			maxline.first=textline.size();
			maxline.second=lineum;
		}
		lines_of_text->push_back(textline);
		lineum++;
	}
	return lines_of_text;
}
system("pause");
return 0;

}

these codes are from the c++ prime third edition and I can't identify so many errores in the compiler.
That really came out of a book? Thats just ugly bad code.
What is it that your trying to get from this example?
Lots of problems with this.

It's missing a couple of required headers:
1
2
3
 
#include <string> 
#include <fstream> 


Several problems with line 7:
1) Invoking the vector template with an allocator parameter, but you have no allocator.
2) What are the () at the end of the line?
3) No semicolon at the end of the line.
4) retrieve_test is never used.

Line 16: Proper return values for main are 0 or 1, not -1.
Line 19: Why is vector being dynamically allocated. That's just wrong.
Line 28. Flat wrong. That should be a simple <.
Line 34: Linenum is misspelled.
Line 36: Trying to return a vector pointer as the result of main. Makes no sense.
And finally, there is a memory leak. The dynamically allocated line_of_text is never released. Maybe line 36 was supposed to be delete, not return.

Apparently no one proofread the code in the book.



my attemp is to store a txt file in line one,line two and so on, and finally calculate the longest line. Honestly it is from the c++ prime
How about this instead:

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

using namespace std;

int main(int arg, char*args[])
{	vector<string>	lines;

	string file_name;
	cout<<"please enter file name:";
	cin>>file_name;
	ifstream infile(file_name.c_str(), ios::in);
	if(!infile)
	{	cerr<<"sorry,can't open the file"<<file_name<<endl;
		exit(EXIT_FAILURE);
	}
	string textline;
	int linenum=0;
	int linelen;
	int maxline = 0;
	int longestline = 0;
	while(getline(infile,textline,'\n'))
	{	linenum++;
		linelen = textline.size();
		lines.push_back (textline);
		if (linelen > maxline)
		{	maxline = linelen; 
			longestline = linenum;
		}
	}
	if (lines.size() == 0)
	{	cout << "The file is empty" << endl;
		exit (EXIT_FAILURE);
	}
	cout << "The longest line is line number: " << linenum << endl;
	cout << lines[linenum-1] << endl;
	system("pause");
	return 0;
}

thanks guys. I will improve this field.
Topic archived. No new replies allowed.