2012-03-06 10 views
0

私は、私が仕事が必要な追加のものを示すためにクラスを出す予定です。私が型キャスティングをしたらうまくいくのでしょうか?それとも、文字列を覚えるだけですか?言葉の手紙

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"); 
     } 
    } 

あなたはどのようにしますか? Like Ifユーザーが's'を入力すると、Spadeになります。

+1

そのようなあなたの場合は、チェックを行ってください: '場合(派閥[0] == 's' ||派閥[0] == 'c' ||派閥[0] == 'h' ||派閥[0] == 'd') '。あなたの質問には関係ありませんが、あなたの人生は醜いものです。 –

+0

pythonチュートリアル –

答えて

1

この行は動作しません:

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

をif文の非常に一部が自身によって評価されます。例: 'c'は常にtrueと評価されるため、この文は効果がありません。あなたは完全にここにswitchステートメントを使用することができ

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

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:また、あなたのデータ型について注意することは、このようなものでなければなりません。たとえばfaction[0]charのようで、文字列ではないと思われるため、faction[0] = "spade";が正常に機能するかどうかはわかりません。

+0

の[辞書](http://docs.python.org/tutorial/datastructures.html#dictionaries)を読んでいただきありがとうございます。しかし、どのように手紙を単語に変換できますか? – JuanDelCarlos

+0

文字列を作成するにはどうすればよいですか?私はそれについて聞いたことがありません。 – JuanDelCarlos

+0

@JuanDelCarlos:それはあなたが入れたい場所に依存します。私はあなたが単語を置く新しい文字列変数を初期化することをお勧めします。 – Constantinius

1

あなたのやり方に条件を付けることはできません。あなたの

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

switchステートメントを使用することができます:factionの最初の文字がS かの最初の文字である場合の条件は、 `派閥は、c または ...このよう

です

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; 
} 

上記のコードでは、別の問題が修正されています。faction[0]を文字列と文字の両方として使用しています。 factionのタイプを指定していないので、上の例では文字列にしました。

3

この目的でstd::mapを使用できます。 charからstd::stringまでのマップを作成し、1つの値をs,c,hおよびdと設定します。それを使用する方法の

小さな例は次のようになります。

あなたの質問に適応し
#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; 
} 

factioncharの配列であると仮定すると:

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); 
} 

今これはでは動作しません。あなたのクラスでは、setLetter関数は1つのcharを期待しているので、letterメンバーもcharです。だから、あなたはstd::stringを取るためにこれらの2つを変更する必要があります。

したい唯一のことは、1つの文字であれば、あなたのletterメンバーは大丈夫ですが、mainであなたのsetLetter呼び出しはあまり意味がありませんので、あなたは、単一の文字で文字列全体を置くことはできません。あなただけのswitchステートメントを使用し、完全なスーツ名に単一の文字からのマッピングだけの手紙を必要としないのであれば:

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; 
} 
+0

これはあまりにも進んでいます。 – JuanDelCarlos

+0

@JuanDelCarlos:行ごとに見ると複雑ではなく、 'map'や' vector'のような標準的なコンテナを使うのは本当に重要です。 (そして 'std :: string'sも使います。)完全なスーツ名の後ではなく、文字だけの場合は、単純なスイッチのバリエーションを追加しました。 – Mat

1
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"; 
}