1

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:

  1. How do you compare two strings? I have tried strcmp and a few others but I always get the error message: cannot convert std::string to const char* for argument 2.

  2. How do you concatenate an int and a string? In Java it was as simple as using the + operator.

  3. In my main function, it says there is no matching function call for Username::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;
    
    
         }
      }
    
4
  • Read what sizeof does. And there's no reason to use a pointer in main. Commented Aug 18, 2013 at 8:08
  • While you're at it, read what std::string can 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. Commented Aug 18, 2013 at 8:11
  • There are enough things wrong in java-translated-to-C++ that you need to understand just how different they really are. See it live here. Commented Aug 18, 2013 at 8:39
  • Wow thanks for the example. It will definitly help me. I am trying to solve this problem exactly:[basic links] (community.topcoder.com/… ) I must use a class and have a method that only excepts an array of strings. Commented Aug 18, 2013 at 8:55

1 Answer 1

2

There are some mistakes in your code :

  • If you want to compare two strings, simply use the operator== : string == string2.

  • If you want to append an int to a string in C++ you can use streams:

#include <sstream>

std::ostringstream oss;
oss << "Justin" << 4;
std::cout << oss.str();
  • You are passing a string* to the function newMember but your prototype doesn't match that:
string *userNames = new string[4];
newMember(userNames, "Justin"); // Call

string newMember(string existingNames, string newName); // Protype

I think it should be: string newMember(string* existingNames, string newName);, no?

  • In the example, your main function is inside your Username class. It is not correct in C/C++. Unlike Java, the main function has to be in the global scope.

  • Finally, you should use const-reference parameter because you don't need to modify the content of them, and you need to copy them anyway:

string newMember(string* existingNames, const string& newName);
//                                      ^^^^^       ^

Are you sure you need something allocated dynamically in the main function?

Sign up to request clarification or add additional context in comments.

7 Comments

I am getting the error:Undefined symbols: "_main", referenced from: start in crt1.10.6.o ld: symbol(s) not found collect2: ld returned 1 exit status But I have my main function in my program. why is it not detecting my main function?
@user2510809 In the example you gave us, it seems that your main function is inside the class Username but unlike java, the main function has to be in the global scope.
@user2510809 I updated my answer to add this to the explanation. Is it working now ?
I am sorry but I don't know how to access newMember from main. It keeps saying: newMember was not declared in this scope. How do I make newMember global?
@user2510809 In fact, in your code, the class Username is useless. You are not using a Username instance here. You should take a look at this : stackoverflow.com/questions/789659/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.