-3
char gamerCentral::getGamerTag( )
{
    switch(gamerTag)
            {

        case '1':  return gamerTag = "Diamond";  
                    break; 
        case '2':  return  gamerTag = "Silver";
                    break; 
        case '3':  return  gamerTag = "Bronze";
                    break; 
        case '4':  return  gamerTag = "Wood";
                    break;    
        default:   return  gamerTag = "Uninstall";

        break; 
            }

 char   gamerTag;

GamerClub::GamerClub(
        char tag)
    {
    gamerTag = tag;


}

I'm trying to return the gamerTag, but it says that it cannot convert to a string. Is their a way to convert the char to a string inside the switch statement?

theDon
  • 1
  • 1
  • 2
    Seems like [The XY Problem](http://xyproblem.info/). Why not use string type to store the strings? – MikeCAT Nov 18 '16 at 15:37
  • You should change the signature of your function. A char is a char, you need something bigger than a `char` here. Closest would be a `const char *`, you can have a look [here](http://stackoverflow.com/questions/7352099/stdstring-to-char) if you want to see more. – Jean-Emmanuel Nov 18 '16 at 15:39
  • How do you expect to be able to assign an entire string to a mere, lowly, `char` variable? – Sam Varshavchik Nov 18 '16 at 15:39
  • What is going on in this code? You're assigning a `const char*` to a `char`, then returning the result of that assignment? – Cory Kramer Nov 18 '16 at 15:39
  • Do NOT do this: `#define char std::string` – MikeCAT Nov 18 '16 at 15:43
  • Its needed to be a char and needs to return as a string for the output of the user. The project is required to input a single input "char" and output the string. Is there anyway I can get the input to be a char and output the message for example "Diamond" a different way? – theDon Nov 18 '16 at 15:44
  • 1. you're not printing. 2. no need to break after a return, it just makes things longer 3. `std::string getGamerTag(char tag)` 4. `std::string tag = getGamerTag(gamerTag)` 5. `cout << tag` – UKMonkey Nov 18 '16 at 15:47

3 Answers3

3

Have your function return a string, it can still operate on the single character as input. Here's an example.

std::string getGamerTag(char t)
{
   switch(t)
   {
      case '1': return "Diamond";
      case '2': return "Silver";
      // ...
   }

   return "";
}

// prints "Silver"
cout << getGamerTag('2');
Chad
  • 18,706
  • 4
  • 46
  • 63
  • string gamerCentral::getGamerTag(char gamerTag ) { switch(gamerTag) { case '1': return gamerTag = "Diamond"; case '2': return gamerTag = "Silver"; case '3': return gamerTag = "Bronze"; case '4': return gamerTag = "Wood"; default: return gamerTag = "Uninstall"; } – theDon Nov 18 '16 at 16:14
  • This is what i'm putting and it still will not convert. – theDon Nov 18 '16 at 16:15
  • `return gamerTag = "Diamond";` _is_ what is wrong. You can't assign the value "Diamond" to a `char`. Look more closely at my answer. – Chad Nov 18 '16 at 17:53
0

use string variable or use char*

char* getGamerTag(int t)
{
   switch(t)
   {
      case 1: return "Diamond";
      case 2: return "Silver";
   }
}
int main()
{
    cout << getGamerTag(2);
}
0

I recommend using an array of strings:

std::string getGamerTag(unsigned int t)
{
  static const char * tag_names[] =
  {
    "Diamond", "Silver", "Bronze", "Wood", "Unistall",
  };
  static const unsigned int name_quantity =
    sizeof(tag_names) / sizeof(tag_names[0]);

  std::string name;
  if ((t > 0) && (t <= name_quantity))
  {
    name = tag_names[t - 1];
  }
  return name;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154