简体   繁体   English

C++ 字符串和 If 语句

[英]C++ Strings and If Statements

The goal is to ask for 2 colors then output the appropriate secondary color(purple,green,orange), I get an error telling me I can't convert standard strings to shorts.目标是要求 2 种颜色,然后输出适当的辅助颜色(紫色、绿色、橙色),我收到一条错误消息,告诉我我无法将标准字符串转换为短裤。

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    short result,col_1,col_2,blue,red,yellow;

    const string str1="Purple.",
    str2="Orange.",
    str3="Green.",
    str4="Incorrect, please re-enter.";

    cout<<"Please enter only 2 of the following colors (yellow,blue,red): ";
    cin>>col_1>>col_2;


    if((col_1==red||blue)&&(col_2==red||blue))
    {result=str1;
    }else{
    result=str4;
}
    if((col_1==red||yellow)&&(col_2==red||yellow))
    {result=str2;
    }else{
    result=str4;
}
    if((col_1==yellow||blue)&&(col_2==yellow||blue))
    {result=str3;
    }else{
    result=str4;
}
    cout<<"Your combined color is: "<<result<<endl;
    //for example red and blue should output : Purple.
    return 0;
}

The error message you saw describes the problem:您看到的错误消息描述了问题:

short result;
const string str1="Purple.";

[...]

result=str1;

result is a short , and str1 is a std::string . result是一个short ,而str1是一个std::string There is no built-in way for C++ to convert a std::string into a short , so the compiler generates that error when you try to make that assignment. C++ 没有将std::string转换为short内置方法,因此当您尝试进行该分配时,编译器会生成该错误。

Perhaps you meant to declare result as a string instead?也许您打算将result声明为字符串?

short is number type, your result is short but your str1 , str2 , str3 , str4 are string , in C++ there is no built-in way to covert string to short (or any to number data type). short是数字类型,您的resultshort但您的str1str2str3str4string ,在 C++ 中,没有内置方法将stringshort (或任何数字数据类型)。 In your situation, you can declare the result as a string.在您的情况下,您可以将result声明为字符串。 And don't convert a string to a number if you don't have to!如果不需要,不要将字符串转换为数字!

Even if you "fix" the bug about data types (short and string), your algorithm is not entirely correct and it won't procedure the right answer.即使您“修复”了有关数据类型(短和字符串)的错误,您的算法也不完全正确,并且不会处理正确的答案。 Take these lines of code as an example:以这几行代码为例:

    if((col_1==red||blue)&&(col_2==red||blue))
    {result=str1; // which is "purple"
    }else{
    result=str4;
}

or, re-written probably:或者,可能重写:

if ((col_1 == "red" || col_1 == "blue") && (col_2 == "red" || col_2 == "blue"))
{
    result = "purple";
}
else
{
    result = str4;
} 

What if col_1 = "red" and col_2 = "red" ?如果col_1 = "red"col_2 = "red"呢? According to this code, the result would be purple (which is unexpected).根据此代码,结果将是purple (这是意料之外的)。 You should modify it a bit:你应该稍微修改一下:

if ((col_1 == "red" && col_2 == "blue") || (col_1 == "blue" && col_2 == "red"))
{
    result = "purple"
}
else
{
    result = str4
}

So, what can you learn from my re-written code?那么,你能从我重新编写的代码中学到什么?

  1. Think about your algorithm carefully.仔细考虑你的算法。 Make sure it is correct before you get to coding.在开始编码之前确保它是正确的。

  2. Find more information about the if-statement and data types.查找有关 if 语句和数据类型的更多信息。 You basically cannot compare or assign an integer (or a short ) to a string.您基本上无法将整数(或short )比较或分配给字符串。

  3. Learn more about how to declare variables: only declare what you need, with the appropriate data type.了解有关如何声明变量的更多信息:仅使用适当的数据类型声明您需要的内容。 You want to output the "color", or theoretically a string, so your result variable should also be a string.您想输出“颜色”,或者理论上是一个字符串,因此您的结果变量也应该是一个字符串。 As you are only able to compare strings to strings, if you want to compare col_1 (or col_2 ) to another color ( red , blue , yellow ), these colors must be declared as strings too.由于您只能将字符串与字符串进行比较,如果要将col_1 (或col_2 )与另一种颜色( redblueyellow )进行比较,则这些颜色也必须声明为字符串。 It makes no sense that you're declaring a short variable name red and compare it to a string because you are comparing an unassigned integer to a string.声明一个short变量名red并将其与字符串进行比较是没有意义的,因为您将未分配的整数与字符串进行比较。 The correct way to do this is either, you don't need any new variable to "store" this information, just put the string to want to compare in double-quote, or to define a variable like this:执行此操作的正确方法是,您不需要任何新变量来“存储”此信息,只需将要比较的字符串放在双引号中,或者像这样定义一个变量:

    string red = "red"

(which I think is completely unnecessary, but whatever) (我认为这是完全没有必要的,但无论如何)

  1. Make sure that all of your input is in lowercase, or you'll have to handle something like "REd" and "reD" both are equals to the color "red".确保您的所有输入都是小写的,否则您将不得不处理诸如“RED”和“reD”之类的东西,它们都等于颜色“red”。 Either compare every single possible way of writing the word "red" or just convert all the words back to lowercase (or uppercase) before the comparison.要么比较每一种可能的写单词“red”的方式,要么在比较之前将所有单词转换回小写(或大写)。

  2. This is just a minor thing, but if you want others to read your code and analyze it for you, make sure that your code is readable and is indented probably.这只是一件小事,但是如果您希望其他人阅读您的代码并为您分析,请确保您的代码可读并且可能缩进。

These are just the basics of programming, so you should practice harder so you won't encounter those bugs in the future.这些只是编程的基础知识,因此您应该更加努力地练习,以免将来遇到这些错误。 Good luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM