JuanDelCarlos
JuanDelCarlos

Reputation: 57

Letter into words

I'll be placing my class to show additional things that I need work with. Would it work if I did typecasting? Or I just have to learn strings?

class NumberBox
{
private:

    int number;
    char letter;

public:

    NumberBox *next_ptr;
    void setNumber(int number)
    {
        this->number = number;
    }

    void setLetter(char letter)
    {
        this->letter = letter;
    }


    int getNumber()
    {
        return number;
    }

    int getLetter()
    {
        return letter;
    }
};

int main()

    cout << "Please Give the faction for the first Card" << endl;
    cin >> faction[0];
    if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')
    {
        if(faction[0] == 's')
        {
            faction[0] = "spade";
            factionHead_ptr->setLetter("spade");
        }
    }

How do you do it? Like If The user input 's' then it would be Spade.

Upvotes: 0

Views: 170

Answers (4)

Mat
Mat

Reputation: 206833

You could use a std::map for this purpose. You would build a map from char to std::string, with one value set of s, c, h and d.

A small example of how to use it would be:

#include <iostream>
#include <map>

typedef std::map<char, std::string> suitmap;

int main()
{
    suitmap suits;
    suits['s'] = "spades";
    suits['h'] = "hearts";
    char in;
    std::cout << "Suit?" << std::endl;
    if (std::cin >> in) {
        suitmap::const_iterator it = suits.find(in);
        if (it != suits.end())
            std::cout << it->second << std::endl;
        else
            std::cout << "not found" << std::endl;
    } else {
        std::cout << "no input" << std::endl;
    }
    return 0;
}

Adapted to your question, assuming faction is an array of chars:

cin >> faction[0];
// see if we have a match for faction[0] in the map
suitmap::const_iterator it = suits.find(faction[0]);
if (it == suits.end()) {
   // no match
   // print an error or something
} else {
   // match! the string is in it->second
   std::string suit = it->second;
   factionHead_ptr->setLetter(suit);
}

Now this won't work at all with your NumberBox class since the setLetter function expects a single char, and you letter member is a char too. So you'd need to change these two to take/be std::strings.

If the only thing you want is one letter, then your letter member is fine, but you can't put a whole string in a single letter, so your setLetter call in main doesn't make much sense. So if you do need just a letter, and not a mapping from a single letter to a full suit name, just use a switch statement:

cin >> faction[0];
switch (faction[0]) {
  case 's': case 'c': case 'h': case 'd':
    factionHead_ptr->setLetter(faction[0]);
    break;
  default:
    // invalid input given, do something about it
    break;
}

Upvotes: 3

Ivaylo Strandjev
Ivaylo Strandjev

Reputation: 70989

switch(functin[0]) {
  case 's': faction = "spade";factionHead_ptr->setLetter("spade");break;
  case 'c': faction = "clubs";factionHead_ptr->setLetter("clubs");break;
  case 'h': faction = "hearts";factionHead_ptr->setLetter("hearts");break;
  case 'd': faction = "diamonds";factionHead_ptr->setLetter("diamonds");break;
  default: cerr<<"wrong suite entered";
}

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409266

You can't chain conditions the way you do. The condition is, if first letter of faction is s or first letter of `faction is c or...

Like this:

if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')

Of you can use a switch statement:

string faction;
char type;

cin >> type;

switch (type)
{
case 's':
    faction = "spade";
    break;
case 'c':
    faction = "clib";
    break;
case 'h':
    faction = "heart";
    break;
case 'd':
    faction = "diamond";
    break;
default:
    cout << "Illegal card type\n";
    break;
}

The above code also fixes another problem you have, you are using faction[0] as both a string and a character. Since you don't specify the type of faction I made it a string in my example above.

Upvotes: 1

Constantinius
Constantinius

Reputation: 35069

this line doesn't work:

if(faction [0] == 's' && 'c' && 'h' && 'd')

since very part of the if statement is evaluated by itself. E.g: 'c' will always evaluate to true, so the statement has no effect. It should be something like this:

if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')

You could perfectly use a switch statement here:

switch(faction[0]) {
case 's':
    factionHead_ptr->setLetter("spade");
    break;
case 'c':
    ...
    break;
...

default:
    // code here if none of s, c, h, d
    break;
}

EDIT: also be careful about your data types. E.g I'm not sure if faction[0] = "spade"; will work out as expected since faction[0] seems to be of type char and not a string.

Upvotes: 1

Related Questions