Jump to content
  • Advertisement
Sign in to follow this  
sheep19

[C++] I'm going mad

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is my code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
	string word = "sheep";

	int size_of_word = word.size();

	int position = word.find ('e');

	cout << "position = "<< position;
	
	system("pause");

	return 0;

}

I am trying to find the position of 'e' in the word "sheep". The result I get is 2 (!). I included algorithm and sorted it, but I get the same result. What's wrong??

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Oluseyi
s h e e p
0 1 2 3 4


(Btw, characters is empty, so sorting it is pointless.)


yeah, I had some other code as well, but I deleted it and forgot to delete that one.

THANKS FOR YOUR HELP PEOPLE !!!

Share this post


Link to post
Share on other sites
I have an other problem now :-/

I want to print the position of the second 'e'. How do I do that?

Thanks in advance!

Share this post


Link to post
Share on other sites
Strings find method can take another argument which is the index to start searching at. So after you find the first 'e', use the index++ as the starting index in another search to get the second 'e'.

Share this post


Link to post
Share on other sites
So, does this mean if I have int position = word.find_first_of('e', 2); I will get 2? Hmm, yes.

But if I write ('e', 3) I get position = 3 .. Shouldn't it have been 1 ??

Share this post


Link to post
Share on other sites
Quote:
Original post by lude
Strings find method can take another argument which is the index to start searching at. So after you find the first 'e', use the index++ as the starting index in another search to get the second 'e'.


I will try it tomorrow, it's too late now. Thanks!

What I want to do, as it's getting a bit confusing, is to find how many 'e' I have and where they are located. (position)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!