C++ str.find(str2) seems to be not working

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



C++ str.find(str2) seems to be not working



right now I'm working on a program but I've run into this issue. Disclaimer; I'm not that experienced with c++ yet.
For my program I'm taking a user input, and comparing a string to that. If the userinput contains, key word here, contains. Doesn't have to equal but contain, the program should return a string. Now I've looked up this issue on the Web and the best way to do this seems by using str.find(str2) now as I understand by reading it, str2 should be the substring looked for in str, but I can't get that to work. So I reversed them str2.find(str). But this only gives of a result if the string is exactly equal. Am I doing anything wrong in my setup maby?


str.find(str2)


str2.find(str)


#include <iostream>
#include <string>

using namespace std;
int main()

string Userinput;
string str2 = "He";
cin >> Userinput;
std::size_t found = str2.find(Userinput);
if (found!=std::string::npos)

std::cout << "found it!" << str2 << 'n';

return 0



I'm looking for any help! Thanks in advance!



I'm sorry for the typos, I'm typing on my phone, I'll check again but I think I fixed them but when my input is He, it says found. When my input is Hello it returns nothing





Your description mentions str and str2, while your code uses str2, userinput, and afk. Can you make them match so it makes sense? You might also read through Minimal, Complete, and Verifiable example and fix your example so it shows the values of the variables you're using and how you're using them.
– Retired Ninja
Aug 12 at 2:04


str


str2


str2


userinput


afk





Show how you read user input. Is there a newline on the end?
– stark
Aug 12 at 2:05





IL fix the mistakes
– TB Gamezz
Aug 12 at 2:11





Your code is still not complete. Also, what input do you use?
– HolyBlackCat
Aug 12 at 2:13






t.cpp:8:15: error: ‘He’ was not declared in this scope
– Anthony Sottile
Aug 12 at 2:20


t.cpp:8:15: error: ‘He’ was not declared in this scope









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard