Strings

How come start_head and end_head is not storing the index of the first occurrence of <head> and </head>? But rather, it basically holding the values of std::string::npos.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <vector>

//"q<head>werq<b>this should be bold</b>qwerq313</head>4adfaerq";

int main ()
{	
	std::string input = "q<head>werq<b>this should be bold</b>qwerq313</head>4adfaerq"; 
	std::size_t start_head = input.find ("<head>", 0); 
	std::size_t end_head = input.find ("</head>", 0); 
	std::cout << start_head; 
	std::cout << end_head;
return 0; 
Last edited on
Could you edit your code so it's complete (the missing final brace!)

But it works on C++ Shell

1
45


Now with added "\n"s and closing brace!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <vector>

//"q<head>werq<b>this should be bold</b>qwerq313</head>4adfaerq";

int main ()
{	
	std::string input = "q<head>werq<b>this should be bold</b>qwerq313</head>4adfaerq"; 
	std::size_t start_head = input.find ("<head>", 0); 
	std::size_t end_head = input.find ("</head>", 0); 
	std::cout << start_head << "\n"; 
	std::cout << end_head   << "\n";
	return 0;
}


Andy
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <vector>

//"q<head>werq<b>this should be bold</b>qwerq313</head>4adfaerq";

int main ()
{	
	std::string input = "q<head>werq<b>this should be bold</b>qwerq313</head>4adfaerq"; 
	std::size_t start_head = input.find ("<head>", 0); 
	std::size_t end_head = input.find ("</head>", 0); 
	std::cout << start_head << ' '; // <-- makes it a bit clearer
	std::cout << end_head;
return 0; 
} // <-- oops 
sigh...

Thank you guys...
Topic archived. No new replies allowed.