+ 2

what is the problem in this code coach?

code coach name: Buddy case 5: still got failed // created by naveed ahmad #include <iostream> #include <string> using namespace std; int main() { string buddy, me; getline(cin, buddy); getline(cin, me); int x = buddy.find(me.at(0)); int y = buddy.find(' '); y++; if (x == y || x == 0) cout << "Compare notes" <<endl; else { cout << "No such luck"; } return 0; }

8th Sep 2020, 12:42 PM
Naveed
Naveed - avatar
14 Answers
+ 4
Consider the following sample input: Amelie Arthur Ben Birgit Your code wouldn't work in this case because 'y' always finds the first space, while 'x' is not necessarily in the first or second name. It would be a lot easier to just omit that space-comparison and instead to only check whether 'x' is actually in the string or not using std::string::npos.
8th Sep 2020, 1:21 PM
Shadow
Shadow - avatar
+ 7
It's really just as simple as if ( x != string::npos ) // x in buddy else // x not in buddy Here's the find() reference: https://en.cppreference.com/w/cpp/string/basic_string/find And here std::string::npos: https://en.cppreference.com/w/cpp/string/basic_string/npos Since you introduced the standard namespace via using namespace std; earlier, you can omit std:: before string::npos. If you want to read a few things about namespaces, see here: https://en.cppreference.com/w/cpp/language/namespace std is just the namespace used by the STL: https://www.sololearn.com/discuss/905774/?ref=app https://www.sololearn.com/discuss/207431/?ref=app https://www.sololearn.com/discuss/570114/?ref=app https://www.sololearn.com/discuss/1456298/?ref=app
9th Sep 2020, 8:09 AM
Shadow
Shadow - avatar
+ 4
Because it's "No such luck", without the exclamation mark.
9th Sep 2020, 1:32 PM
Shadow
Shadow - avatar
+ 4
Done, Brothers thanks for helping me out❣️
9th Sep 2020, 3:17 PM
Naveed
Naveed - avatar
+ 3
As I tried to explain before, you really only need to check if the first letter of your name is present in the "buddy" string or not. find() will return some valid index if its argument is contained in the string calling the method, and std::string::npos otherwise. The connection should be pretty obvious, shouldn't it?
8th Sep 2020, 10:01 PM
Shadow
Shadow - avatar
+ 2
Naveed Ahmad Shadow's method is right bro ...
9th Sep 2020, 7:48 AM
Ipang
+ 2
Ipang bro Now i did like this now case 1 and 5 are fixed but other cases failed :( #include <iostream> #include <string> using namespace std; int main() { string buddy, me; getline(cin, buddy); getline(cin, me); size_t x = buddy.find(me.at(0)); if( x != string::npos) cout << "Compare notes"; else cout << "No such luck!"; cout << endl; return 0; }
9th Sep 2020, 11:48 AM
Naveed
Naveed - avatar
+ 1
Ipang 🤗
9th Sep 2020, 5:14 AM
Naveed
Naveed - avatar
+ 1
Ipang but I didn't understand that.....
9th Sep 2020, 7:49 AM
Naveed
Naveed - avatar
+ 1
How can I change that in my code and where did I put that STD::.....
9th Sep 2020, 7:50 AM
Naveed
Naveed - avatar
+ 1
Shadow can you add that method in my code so that I can understand?
9th Sep 2020, 7:52 AM
Naveed
Naveed - avatar
+ 1
Naveed Ahmad Taking Shadow's example, there's very little change needed. There's no need for variable <y>, all that is needed would be like size_t x = buddy.find(me.at(0)); if( x != string::npos) cout << "Compare notes"; else cout << *No such luck!"; cout << endl; `string::find` method returns a special constant string::npos when no match was found. Big Thanks Shadow 🙏
9th Sep 2020, 9:08 AM
Ipang
+ 1
Naveed Ahmad bro My bad, no exclamation is there. Minor thing but when it comes to challenges. important to follow the requirements 👌
9th Sep 2020, 3:12 PM
Ipang
0
Shadow now what can I do?
8th Sep 2020, 3:39 PM
Naveed
Naveed - avatar