首先要做的事情:我知道这段代码太长了,可以缩短很多。但是,我不想帮助如何缩短它,我只是想了解一些基础知识,而我现在的问题是运算符和存储值。正如您可能从代码中看到的那样,我正在尝试使用一堆 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;
}