Problem with a 'string' 'for' loop.

Hello. I have a small issue, I guess. My code works, but it only gets the first string in my array. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
				std::size_t found;
				const int SIZE = 2;
				std::string Array[SIZE] = { "item1", "item2"};
				
				for(int i = 0; i < SIZE; i++)
				{
					found = msg.find(Array[i]);
					if(found != std::string::npos)
					{
					        // Cannot continue
						return;
					}
					else
					{
						Message(msg);
						return; // Return here so the message isn't spammed
					}
				}


Now, I want to know how I can get each string in my array. Thanks!
Remove the two return statements.
If I do, the word will seek through into the chat log. If the return isn't there, players won't be able to send their message.
What chat log? Why are they not able to send?
Last edited on
This is for my game. When I take out the 'return', it will cause the message to be displayed and ALSO, the error will display too.. when it should be reversed.
I don't know what is going on but a return statement will end the whole function.
How specific do I have to be, lol? I pretty much explained everything 'that is going on'. Anyway, I'll try and take the return statement out and see if I can fix it.
I've often wondered about arrays of strings. The reason being is that the [] operator (if you can call it an operator) already has an action when associated with a string. Specifying an index in this will return that particular character of the string. There seems to therefore be a conflict.

For instance:
1
2
3
std::string sTest = "Hello";
std::cout << "First letter: " << sTest[0] << std::endl;
std::cout << "Second letter: " << sTest[1];

will output

First letter: H
Second letter: e


So in this case:
1
2
std::string sTest[2] = {"Hello" , "World"}
std::cout << sTest[0] << std::endl << sTest[1];

What would the output be?
Last edited on
the output would be

Hello
World


if you want the single character you'll probabaly need to do something like

cout << sTest[0][0]; // give me the first string's first character

If I'm not mistaken.
Last edited on
Topic archived. No new replies allowed.