简体   繁体   English

以下涉及输入和字符串的C ++代码有什么问题?

[英]What's wrong with the following C++ code involving input and string?

#include <iostream>

typedef int temperature;
temperature celsiustemperature[4];

int main()
{
    using namespace std;
    cout << "Enter a start temperature in celsius: ";
    cin >> celsiustemperature[0];
    cout << "Enter an end temperature in celsius: ";
    cin >> celsiustemperature[1];
    cout << "You printed " << celsiustemperature[0] << " and " << celsiustemperature[1] << "." << " Is this correct?" << endl;
    char szYesOrNo;
    cin >> szYesOrNo;
    switch (szYesOrNo)
    {
        case "yes":
               cout << "win";
               break;
        case "no":
              cout << "winner";
              break;
     }
     return 0;
}

I cannot figure out what is wrong with the following code. 我无法弄清楚以下代码有什么问题。 Sorry for not adding comments; 很抱歉没有添加评论; I want it to print win if the user inputs yes and winner if user inputs no . 我希望它在用户输入yes打印win ,在用户输入no打印winner

This won't compile, because szYesOrNo is a char , and you are comparing it with string literals in your switch statement. 这不会编译,因为szYesOrNochar ,并且您正在将它与switch语句中的字符串文字进行比较。 String literals are of type const char[] , which cannot be directly compared to char . 字符串文字的类型为const char[] ,不能直接与char比较。

Use std::string instead of a char : 使用std::string代替char

std::string szYesOrNo;

This will also force you to remove the switch , because switch cannot operate on a string value (also notice, that your switch does not have a default case, so it won't handle incorrect input). 这也将迫使您删除switch ,因为switch不能对string值进行操作(还请注意,您的switch没有default大小写,因此它不会处理错误的输入)。 Just do it this way: 就是这样:

if (szYesOrNo == "yes")
{
    cout << "win";
}
else if (szYesOrNo == "no")
{
     cout << "winner";
}
else
{
    // Handle wrong input...
}
char szYesOrNo;

This is a single character. 这是一个字符。

switch (szYesOrNo)
{
    case "yes":

This will compare the value of the single character with the address of the string "yes" , which isn't what you want. 这会将单个字符的值与字符串"yes"地址进行比较,这不是您想要的。

Either input to a string, or compare chars. 输入字符串或比较字符。

To compare chars: 比较字符:

switch (szYesOrNo)
{
    case 'y':

To compare strings, you can't use a switch statement. 要比较字符串,不能使用switch语句。 You could use nested if/else : 您可以使用嵌套的if/else

string szYesOrNo;

if(szYesOrNo == "yes") {
    cout << "win";
} else if(szYesOrNo == "no") {
    cout << "winner";
}

You can use == when comparing a string to a char[] (which is what "yes" is). stringchar[]比较时可以使用== (这是"yes" )。 string makes sure the contents are compared rather than just the addresses. string可确保对内容进行比较,而不仅仅是地址。

You can't switch on a string. 您无法打开字符串。 Only on an integer-like type (ints, enums, chars, longs). 仅适用于整数类型(int,enum,char,longs)。

The main problem with your code is that you declare a variable as char szYesOrNo; 代码的主要问题是将变量声明为char szYesOrNo; which can only hold ONE symbol, such as a letter, but then expect the character to enter a whole word. 只能包含一个符号(例如字母),但是期望该字符输入整个单词。 You should use a string to do this instead. 您应该使用string来代替。 When you fix that, you will need to use if statements instead of a switch statement to make decisions. 解决此问题时,需要使用if语句而不是switch语句来进行决策。

First of all, szYesOrNo is a single char and so can only contain one character. 首先, szYesOrNo是一个char ,因此只能包含一个字符。 When you do cin >> szYesOrNo; cin >> szYesOrNo; , you are only reading y or n . ,您只读yn

Second, you're trying to use a switch statement to compare this single char to the string literals "yes" and "no" . 其次,您尝试使用switch语句将此单个char与字符串文字"yes""no" This comparison doesn't make sense. 这种比较没有道理。 The string literals are of type "array of N const char ". 字符串文字的类型为“ N const char数组”。

Instead, use std::string like so: 而是使用std::string像这样:

string szYesOrNo;
cin >> szYesOrNo;
if (szYesOrNo == "yes") {
  cout << "win";
} else if (szYesOrNo == "no") {
  cout << "winner";
}

szYesOrNo是字符,因此如果是字符串(3个字符),则不能切换它

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

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