+ 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; }
14 odpowiedzi
+ 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.
+ 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
+ 4
Because it's "No such luck", without the exclamation mark.
+ 4
Done, Brothers thanks for helping me out❣️
+ 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?
+ 2
Naveed Ahmad
Shadow's method is right bro ...
+ 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;
}
+ 1
Ipang 🤗
+ 1
Ipang but I didn't understand that.....
+ 1
How can I change that in my code and where did I put that STD::.....
+ 1
Shadow can you add that method in my code so that I can understand?
+ 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 🙏
+ 1
Naveed Ahmad bro
My bad, no exclamation is there. Minor thing but when it comes to challenges. important to follow the requirements 👌
0
Shadow now what can I do?