0

まず最初に: このコードは長すぎるので、かなり短縮できることはわかっています。ただし、それを短縮する方法については助けたくありません。いくつかの基本を理解しようとしているだけで、現在の問題は演算子と値の保存にあります。おそらくコードからわかるように、一連の if ステートメントを使用して特定の値を変数に格納し、それらの値を最後に文字列にまとめて表示しようとしています。コンパイラは私のコードを気に入らず、演算子関連のエラーを大量に表示します。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string type = "";
    string color = "";
    string invTypeCode = "";
    string invColorCode = "";
    string fullCode = "";

    cout << "Use this program to determine inventory code of furniture.";
    cout << "Enter type of furniture: ";
    cin >> type;

    if (type.length() == 1)
    {
        if (type == "1" or "2")
        {
         if (type == "1")
         { 
              invTypeCode = "T47" << endl;
              }
         if (type == "2")
         { 
              invTypeCode = "C47" << endl;
              }
              else 
              cout << "Invalid inventory code." << endl;
    }}

    else
         cout << "Invalid inventory code." << endl;

    cout << "Enter color code of furniture: ";
    cin >> color;

    if (color.length() == 1)
    {
        if (color == "1" or "2" or "3")
        {
         if (color == "1")
         { 
              invColorCode = "41" << endl;
              }
         if (type == "2")
         { 
              invColorCode = "25" << endl;
              }
         if (type == "3")
         { 
              invColorCode = "30" << endl;
              }
              else 
              cout << "Invalid inventory code." << endl;
    }}

    else
         cout << "Invalid inventory code." << endl;

    fullCode = invTypeCode + invColorCode;
    cout << fullcode << endl;

    system("pause"); 
    return 0;
}
4

3 に答える 3

5
if (color == "1" or "2" or "3")

する必要があります

if (color == "1" or color == "2" or color == "3")

または、通常のスタイルを好む場合は、

if (color == "1" || color == "2" || color == "3")

演算子||orは同じですが||、バージョンが古いため、ほとんどの人が使用します。

于 2013-04-23T19:35:37.977 に答える
3

入力ストリームと出力ストリームを適切に使用していないようです。例えば:

         invTypeCode = "T47" << endl;

通常、endl と << を使用している場合、 << 演算子の lhs 側は std::ostream です。この場合、lhs は文字列であるため、コンパイラは文句を言います。この場合、あなたが本当に望んでいるのは、「T47」を「T47\n」に置き換え、「<< endl」を削除することです。

また、「フルコード」への最後の参照で大文字と小文字が間違っています。大文字の「C」を含む「fullCode」である必要があります。C++ では大文字と小文字が区別されます。

于 2013-04-23T19:41:58.413 に答える
1

また、次のようなステートメントは機能しません。

         invColorCode = "25" << endl;

で何を達成しようとしているのかよくわかりませんendl

于 2013-04-23T19:37:52.320 に答える