带有 if 语句的 C++ 字符串变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21339750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:36:23  来源:igfitidea点击:

C++ string variables with if statements

c++stringif-statement

提问by judonomi

I've tried re-defining these variables in every imaginable way possible to try and get this line to work. I'm just going to give one example here to represent what's troubling me.

我已经尝试以各种可以想象的方式重新定义这些变量,以尝试使这条线起作用。我只想在这里举一个例子来代表困扰我的事情。

double const FRAME_COST = 45.00;
string input;
char yes,
     no;
int frames;


cout << "Do you want additional frames? Type yes/no:  ";
cin  >> input;

    if (input == yes){
       cout << "How many?"
       cin >> frames;
       frames = frames * FRAME_COST;
       }

// The problem is in **the if statement**
// I need to use a string not a bool (according to my professor)
// I can't get the compiler to recognize the **if statement**
// I realize this isn't practical, but he always throws curve balls.

回答by dasblinkenlight

Your current program has undefined behavior, because yesand noare character variables that have not been initialized, and you are using one of them in a comparison.

您当前的程序具有未定义的行为,因为yesno是尚未初始化的字符变量,并且您正在比较中使用其中之一。

To fix, remove the declarations of yesand no(you do not need them), and use a string literal instead:

要修复,请删除yesand的声明no(您不需要它们),并使用字符串文字代替:

if (input == "yes") {
    ...
}

Note: your comparison may be too strict, because it is case-sensitive. It will take a yes, but it would not take a Yesor a YESas an answer. To address this you may want to convert the input string to lower case before the comparison.

注意:您的比较可能过于严格,因为它区分大小写。它将需要 a yes,但它不会将 aYes或 aYES作为答案。为了解决这个问题,您可能需要在比较之前将输入字符串转换为小写。

回答by IdahoSixString

const string YES = "yes";
const string NO = "no";
const double FRAME_COST = 45.0;


int main()
{
    string input;
    double frames;
    cout << "Hello World" << endl; 
    cin >> input;

    if(input == YES)
    {
        cout << "How many?" << endl;
        cin >> frames;
        frames = frames * FRAME_COST;
        cout << frames << endl;
   }

   return 0;
}

回答by Lambda λ

input == yesneeds to be input == "yes"the quotes let the compiler know it's a string and not an identifier. I also think thismight be helpful.

input == yes需要是input == "yes"引号让编译器知道它是一个字符串而不是一个标识符。我也认为可能会有所帮助。

回答by Lightness Races in Orbit

Just having a charnamed "yes" and another charname "no" is not sufficient, especially as you never actually gave them any values. I think you meant to write:

仅仅char命名为“是”和另一个char名称“否”是不够的,尤其是因为您实际上从未给过它们任何值。我想你的意思是写:

if (input == "yes") {

回答by Zac Howland

You need to do the comparison with a string or character array.

您需要与字符串或字符数组进行比较。

if (input == yes)

This line does nothing as yesis a character pointer that is never initialized. It should be

这一行就像yes一个从未初始化的字符指针一样什么都不做。它应该是

if (input == "yes")

And you do not need the yesvariable (alternatively, you can declare a constant string with the values to check: e.g. const std::string YES("yes");)

并且您不需要该yes变量(或者,您可以声明一个带有要检查的值的常量字符串:例如const std::string YES("yes");

Note, you should probably also account for case-sensitivity.

请注意,您可能还应该考虑区分大小写。

Additionally, you are multiplying an integer framesby a double FRAME_COST(presumably to get the total cost?). This will result in a truncated integer value since you are storing it in an int. If you want the cost to be in dollars and cents, you should store it in a doubleor float:

此外,您将整数乘以framesFRAME_COST精度(大概是为了得到总成本?)。这将导致截断的整数值,因为您将其存储在int. 如果您希望成本以美元和美分为单位,您应该将其存储在 a doubleor 中float

double cost = frames * FRAME_COST;

回答by Klaim

yesand noshould be string constants (if you want to make them perfectly match with the input), either const std::stringor const char*(or auto) but you have to assigh a value.

yesandno应该是字符串常量(如果你想让它们与输入完全匹配),const std::string或者const char*(或自动),但你必须分配一个值。

double const** FRAME_COST = 45.00;
string input;
const char* yes = "yes"
const char* no = "no";
int frames;


cout << "Do you want additional frames? Type yes/no:  ";
cin  >> input;

    if (input == yes){ // exact comparison (maybe add a space trim of input?)
       cout << "How many?"
       cin >> frames;
       frames = frames * FRAME_COST;
       }

回答by Bob

Instead of creating an if statement for only one input, is there a way of doing it with multiple inputs for one if statement without having to create several if statements?

有没有一种方法可以为一个 if 语句使用多个输入而不需要创建多个 if 语句,而不是只为一个输入创建一个 if 语句?

for example...

例如...

string input;
cout << "Are you Bob?";
if (input == "yes", "no", "maybe"){

cout << "Sure...";

}else {
cout << "CANNOT COMPUTE";
}

Every time I try this, the input can be anything, and it will act as if I said "yes", "no", or "maybe".

每次我尝试这个时,输入可以是任何东西,它会表现得好像我说“是”、“否”或“可能”。