Line 180-183 NEED HELP Vector Element ERASE

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include<iostream> 
#include<fstream>
#include<vector>
#include<string>
#include<cmath>

#define ifile "input1.txt"
#define ofile "out.txt"

using namespace std;
string scan( ifstream&);//prototype
void findVector(string, vector<string>&, ofstream&);
void openTagStow(string, vector<string>&,ofstream&);//prototype
void closeTagStow(string, vector<string>&,ofstream&);//prototype
void arrayStow(string, int, string[],ofstream&);//prototype
string openTagScan(string, ifstream&);//prototype
string closedTagScan(string, ifstream&);//prototype



string openTagScan(ifstream&infile) {
	string temp, nothing, hold;
	getline(infile, temp, '<');
	getline(infile, hold, '>');

	if (hold == "HTML") {
		return '<' + hold + '>';
	}
	else if (hold == "HEAD") {
		return '<' + hold + '>';
	}
	else if (hold == "TITLE") {
		return '<' + hold + '>';
	}
	else if (hold == "BODY") {
		return '<' + hold + '>';
	}
	else if (hold == "P") {
		return '<' + hold + '>';
	}
	else if (hold == "IMG SRC='http://image.jpeg'") {
		return '<' + hold + '>';
	}
	else if (hold == "CENTER") {
		return '<' + hold + '>';
	}
	else if (hold == "BR") {
		return '<' + hold + '>';
	}
	else if (hold == "H1") {
		return '<' + hold + '>';
	}
	else if (hold == "H2") {
		return '<' + hold + '>';
	}
	else if (hold == "H3") {
		return '<' + hold + '>';
	}
	else if (hold == "H4") {
		return '<' + hold + '>';
	}
	else if (hold == "H5") {
		return '<' + hold + '>';
	}
	else if (hold == "H6") {
		return '<' + hold + '>';
	}
	else
		return nothing;

}
string closedTagScan(ifstream&infile) {
	string temp, nothing, hold;
	getline(infile, temp, '<');
	getline(infile, hold, '>');

	if (hold == "/HTML") {
		hold = "HTML";
		return '<' + hold + '>';
	}
	else if (hold == "/HEAD") {
		hold = "HEAD";
		return '<' + hold + '>';
	}
	else if (hold == "/TITLE") {
		hold = "TITLE";
		return '<' + hold + '>';
	}
	else if (hold == "/BODY") {
		hold = "BODY";
		return '<' + hold + '>';
	}
	else if (hold == "/P") {
		return '<' + hold + '>';
	}
	else if (hold == "/IMG SRC='http://image.jpeg'") {
		return '<' + hold + '>';
	}
	else if (hold == "/CENTER") {
		hold = "CENTER";
		return '<' + hold + '>';
	}
	else if (hold == "/BR") {
		return '<' + hold + '>';
	}
	else if (hold == "/H1") {
		hold = "H1";
		return '<' + hold + '>';
	}
	else if (hold == "/H2") {
		hold = "H2";
		return '<' + hold + '>';
	}
	else if (hold == "/H3") {
		hold = "H3";
		return '<' + hold + '>';
	}
	else if (hold == "/H4") {
		hold = "H4";
		return '<' + hold + '>';
	}
	else if (hold == "/H5") {
		hold = "H5";
		return '<' + hold + '>';
	}
	else if (hold == "/H6") {
		hold = "H6";
		return '<' + hold + '>';
	}
	else
		return nothing;

}
//passing in vector and valid string..in which i want to store into vector
//open stores open tags into vector  Via Close does for its on vector
void openTagStow(string open, vector<string>& openVector,ofstream&outfile) {
	openVector.push_back(open);
	
	cout << "Open : " << openVector.back()<<endl;

}
void closeTagStow(string close, vector<string>&closeVector, ofstream&outfile) {
	//openVector.insert(openVector.begin(),open;
	//cout << openVector.begin() << endl; get error no mathes these operands

	closeVector.push_back(close);
	cout << "Close : " << closeVector.back() << endl;

}

void arrayStow(string hold, int i, string arr[],ofstream&outfile) {//takes string stores into array(works)

	arr[i] = hold;
	cout << arr[i] << endl;//verified being stored correctly
	outfile << arr[i] << endl;

}
string scan( ifstream&infile) {//scans file for specific <P><BR><IMG...> (works)
	string hold,temp;
	string nothing;


	getline(infile, temp, '<');
	getline(infile, hold, '>');

	if (hold == "P") {
		return '<' + hold + '>';
	}
	else if (hold == "BR") {
		return '<' + hold + '>';
	}
	else if (hold == "IMG SRC='http://image.jpeg'")
	{
		return '<' + hold + '>';
	}
	else
		return nothing; //had nothing

}//How to check file if it is empty?  How to compare to vectors?
void findVector(string C, vector<string>&openVector,ofstream&outfile) {
	string result;
	result = find(openVector.begin(), openVector.end(), C);
	openVector.erase(result);  //ERROR HERE got an error trying to erase the matching tag
	
}
int main() {
	ifstream infile;
	ofstream outfile;
	string P = "",O = "",C = "";
	int i = 0;
	string arr[10] = {};//jut an 
	infile.open("input1.txt");
	if (infile.fail()) {
		cout << "Error input file will not open ";
	}
	outfile.open("out.txt");
	if (outfile.fail()) {
		cout << "Error output file will not open ";
	}
	infile >> P;
	
	vector<string> openVector, closeVector;

	while (!infile.eof()) {
		P = scan(infile);
        if (!infile.eof() && !P.empty())
		{
			arrayStow(P, i, arr,outfile);
			i++;
		}
		
		}
	infile.close();
	outfile.close();
	infile.open("input1.txt");
	outfile.open("out.txt");
	while (!infile.eof()) {
		O = openTagScan(infile);
		if (!infile.eof() && !O.empty())
		{
			openTagStow(O, openVector,outfile);
		}
		
	}
	infile.close();
	outfile.close();
	infile.open("input1.txt");
	outfile.open("out.txt");
	while (!infile.eof()) {
		C = closedTagScan(infile);
		if (!infile.eof() && !C.empty())
		{
			closeTagStow(C, closeVector,outfile);
			findVector(C, openVector,outfile);
		}
		
	}
	infile.close();
	outfile.close();
	system("pause");
	return 0;
}
Last edited on
Consider using std::stack<> for this. Something like this:

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
#include <iostream>
#include <string>
#include <stack>
#include <sstream>
#include <iomanip>

bool parse_stream( std::istream& stm )
{
    std::stack<std::string> tag_stack ; // http://en.cppreference.com/w/cpp/container/stack

    std::string token ;
    while( std::getline( stm >> std::ws, token, '<' ) && std::getline( stm, token, '>' ) )
    {
        if( !token.empty() )
        {
            if( token.front() != '/' ) tag_stack.push(token) ; // begin tag
            else // end tag
            {
                if( tag_stack.top() == token.substr(1) ) tag_stack.pop() ;
                else
                {
                    std::cerr << "*** mismatched tag \"<" << token << ">\"\n" ;
                    return false ;
                }
            }
        }
    }

    return tag_stack.empty() ;
}

void test( const std::string& text )
{
    std::cout << std::quoted(text) << '\n' ;
    std::istringstream file(text) ;
    if( parse_stream(file) ) std::cout << "all tags were matched\n\n";
    else std::cerr << "*** parse error\n\n" ;
}

int main()
{
    for( std::string str : {
                                "<html><head> <h>something</h> <body>something more</body> </head></html>",
                                "<html><head> <h>something</h> <body>something more</body> </hehe></html>",
                                "<html><head> <h>something</h> <body>something <b>more</body> </head></html>",
                                "<html><head> <h>something</h> <body>something <b>more</b></body> </head></html>",
                            }
       ) test(str) ;
}

"<html><head> <h>something</h> <body>something more</body> </head></html>"
all tags were matched

"<html><head> <h>something</h> <body>something more</body> </hehe></html>"
*** mismatched tag "</hehe>"
*** parse error

"<html><head> <h>something</h> <body>something <b>more</body> </head></html>"
*** mismatched tag "</body>"
*** parse error

"<html><head> <h>something</h> <body>something <b>more</b></body> </head></html>"
all tags were matched

http://coliru.stacked-crooked.com/a/fd75c5aeb5e27ddb
http://www.cplusplus.com/reference/vector/vector/erase/

A quick google search and I found your answer. Do some research first, saves yourself times.

Edit: You can read this one too - http://www.cplusplus.com/forum/beginner/99524/
Last edited on
wow how did you come up with that, i enjoy programming but your code is only 49lines...and mine doesnt work so i guess i can not do it this way.
I am a little lost at the std:: no idea what that translates to
> I am a little lost at the std:: no idea what that translates to

See: http://www.cplusplus.com/forum/beginner/142171/#msg750694
Topic archived. No new replies allowed.