I am implementing a string matching algorithm for a username database. My method takes an existing Username database and a new username that the person wants and it checks to see if the username is taken. if it is taken the method is supposed to return the username with a number that isn't taken in the database.
Example:
"Justin","Justin1", "Justin2", "Justin3"
Enter "Justin"
Returns "Justin4" since Justin and Justin with the numbers 1 through 3 are already taken.
I have already written this code in Java, and now I am writing it in C++ for practice. I have a few problems though:
How do you compare two strings? I have tried
strcmpand a few others but I always get the error message: cannot convertstd::stringtoconst char*for argument 2.How do you concatenate an
intand astring? In Java it was as simple as using the + operator.In my
mainfunction, it says there is no matching function call forUsername::NewMember(std::string, std::string). hy does it not recognize newMember in main?#include<iostream> #include<string> using namespace std; class Username { public: string newMember(string existingNames, string newName){ bool found = false; bool match = false; string otherName = NULL; for(int i = 0; i < sizeof(existingNames);i++){ if(strcmp(existingNames[i], newName) == 0){ found = true; break; } } if(found){ for(int x = 1; ; x++){ match = false; for(int i = 0; i < sizeof(existingNames);i++){ if(strcmp(existingNames[i],(newName + x)) == 0){ match = true; break; } } if(!match){ otherName = newName + x; break; } } return otherName; } else return newName; } int main(){ string *userNames = new string[4]; userNames[0] = "Justin"; userNames[1] = "Justin1"; userNames[2] = "Justin2"; userNames[3] = "Justin3"; cout << newMember(userNames, "Justin") << endl; delete[] userNames; return 0; } }
sizeofdoes. And there's no reason to use a pointer inmain.std::stringcan do for you (answers your "how to compare strings" question in the process), and give your runtime-memory-manager a break by learning the joys of a const-reference parameter when you don't need to modify the content or make temp copies.