Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么本案例陈述书需要一个“;如有其他",;而不仅仅是一个;如果“是”的话;? 我是C++新手,我正在通过一个PDF教程来工作,这让我开始了一些基本的东西。我在写一个简单的案例程序,我经历了一些奇怪的事情 #include "pch.h" #include <iostream> #include <string> using namespace std; enum string_feeling { eGood, eBad, eOk, }; string_feeling hashit(string const& feeling) { if (feeling == "Good" || feeling == "good" || feeling == "GOOD") { return eGood; } if (feeling == "Bad" || feeling == "bad" || feeling == "BAD") { return eBad; } if (feeling == "Ok" || feeling == "ok" || feeling == "OK") { return eOk; } else cout << ""; } int main() { string username; cout << "Hello! Please enter your first name here: \n"; cin >> username; cout << "Hello, " << username << "!\n"; cout << "How are you today? "; string feeling; cin >> feeling; cout << endl; switch (hashit(feeling)) { case eGood: cout << "That's great!"; break; case eBad: cout << "I hope you are happy soon!"; break; case eOk: cout << "That's good."; break; default: cout << "Ok."; } } #包括“pch.h” #包括 #包括 使用名称空间std; 枚举字符串{ 伊戈德, 伊巴德, eOk, }; string_feeling hashit(字符串常量和感觉){ 如果(感觉=“好”| |感觉=“好”| |感觉=“好”){ 返回eGood; } 如果(感觉=“坏”| |感觉=“坏”| |感觉=“坏”){ 返回eBad; } 如果(感觉=“好”| |感觉=“好”| |感觉=“好”){ 返回eOk; } 否则不能输入用户名; 无法_C++_Switch Statement - Fatal编程技术网

为什么本案例陈述书需要一个“;如有其他",;而不仅仅是一个;如果“是”的话;? 我是C++新手,我正在通过一个PDF教程来工作,这让我开始了一些基本的东西。我在写一个简单的案例程序,我经历了一些奇怪的事情 #include "pch.h" #include <iostream> #include <string> using namespace std; enum string_feeling { eGood, eBad, eOk, }; string_feeling hashit(string const& feeling) { if (feeling == "Good" || feeling == "good" || feeling == "GOOD") { return eGood; } if (feeling == "Bad" || feeling == "bad" || feeling == "BAD") { return eBad; } if (feeling == "Ok" || feeling == "ok" || feeling == "OK") { return eOk; } else cout << ""; } int main() { string username; cout << "Hello! Please enter your first name here: \n"; cin >> username; cout << "Hello, " << username << "!\n"; cout << "How are you today? "; string feeling; cin >> feeling; cout << endl; switch (hashit(feeling)) { case eGood: cout << "That's great!"; break; case eBad: cout << "I hope you are happy soon!"; break; case eOk: cout << "That's good."; break; default: cout << "Ok."; } } #包括“pch.h” #包括 #包括 使用名称空间std; 枚举字符串{ 伊戈德, 伊巴德, eOk, }; string_feeling hashit(字符串常量和感觉){ 如果(感觉=“好”| |感觉=“好”| |感觉=“好”){ 返回eGood; } 如果(感觉=“坏”| |感觉=“坏”| |感觉=“坏”){ 返回eBad; } 如果(感觉=“好”| |感觉=“好”| |感觉=“好”){ 返回eOk; } 否则不能输入用户名; 无法

为什么本案例陈述书需要一个“;如有其他",;而不仅仅是一个;如果“是”的话;? 我是C++新手,我正在通过一个PDF教程来工作,这让我开始了一些基本的东西。我在写一个简单的案例程序,我经历了一些奇怪的事情 #include "pch.h" #include <iostream> #include <string> using namespace std; enum string_feeling { eGood, eBad, eOk, }; string_feeling hashit(string const& feeling) { if (feeling == "Good" || feeling == "good" || feeling == "GOOD") { return eGood; } if (feeling == "Bad" || feeling == "bad" || feeling == "BAD") { return eBad; } if (feeling == "Ok" || feeling == "ok" || feeling == "OK") { return eOk; } else cout << ""; } int main() { string username; cout << "Hello! Please enter your first name here: \n"; cin >> username; cout << "Hello, " << username << "!\n"; cout << "How are you today? "; string feeling; cin >> feeling; cout << endl; switch (hashit(feeling)) { case eGood: cout << "That's great!"; break; case eBad: cout << "I hope you are happy soon!"; break; case eOk: cout << "That's good."; break; default: cout << "Ok."; } } #包括“pch.h” #包括 #包括 使用名称空间std; 枚举字符串{ 伊戈德, 伊巴德, eOk, }; string_feeling hashit(字符串常量和感觉){ 如果(感觉=“好”| |感觉=“好”| |感觉=“好”){ 返回eGood; } 如果(感觉=“坏”| |感觉=“坏”| |感觉=“坏”){ 返回eBad; } 如果(感觉=“好”| |感觉=“好”| |感觉=“好”){ 返回eOk; } 否则不能输入用户名; 无法,c++,switch-statement,C++,Switch Statement,在启用警告的情况下编译程序,例如g++-Wall-Wextra-Werror,它甚至不会编译,因为string\u-feeling hashit(string-const&feeling)在所有情况下都不会返回值 编译未启用警告的代码肯定会浪费时间。当hashit函数中的三个if语句中的任何一个条件均未变为true时,函数中不会执行任何return语句并调用未定义的行为 (引用6.6.3返回声明) 从函数的末尾流出相当于没有值的返回;这会导致值返回函数中出现未定义的行为 为了避免这种情况,您应该

在启用警告的情况下编译程序,例如
g++-Wall-Wextra-Werror
,它甚至不会编译,因为
string\u-feeling hashit(string-const&feeling)
在所有情况下都不会返回值


编译未启用警告的代码肯定会浪费时间。

hashit
函数中的三个
if
语句中的任何一个条件均未变为true时,函数中不会执行任何
return
语句并调用未定义的行为

(引用6.6.3返回声明)

从函数的末尾流出相当于没有值的返回;这会导致值返回函数中出现未定义的行为

为了避免这种情况,您应该向
enum

enum string_feeling {
    eGood,
    eBad,
    eOk,
    eOther // add this
};
并在不满足任何条件时将其退回

string_feeling hashit(string const& feeling) {
    if (feeling == "Good" || feeling == "good" || feeling == "GOOD") {
    return eGood;
    }
    if (feeling == "Bad" || feeling == "bad" || feeling == "BAD") {
        return eBad;
    }
    if (feeling == "Ok" || feeling == "ok" || feeling == "OK") {
        return eOk;
    } 
    else cout << "";
    return eOther; // add this
}
string\u feeling hashit(字符串常量和感觉){
如果(感觉=“好”| |感觉=“好”| |感觉=“好”){
返回eGood;
}
如果(感觉=“坏”| |感觉=“坏”| |感觉=“坏”){
返回eBad;
}
如果(感觉=“好”| |感觉=“好”| |感觉=“好”){
返回eOk;
} 

否则不能如果不
如果
条件满足,
hashit
转到


else cout您必须始终返回一个值,否则行为未定义

如果无法修改枚举以添加未知感觉的案例,则可以修改hashit以在感觉有效时返回true,在这种情况下,使用相应的枚举值设置输出参数,否则在不设置输出参数的情况下返回false:

#include <iostream>
#include <string>

using namespace std;

enum string_feeling {
    eGood,
    eBad,
    eOk,
};

bool hashit(string const& feeling, string_feeling & r) {
  if (feeling == "Good" || feeling == "good" || feeling == "GOOD") {
    r = eGood;
  }
  else if (feeling == "Bad" || feeling == "bad" || feeling == "BAD") {
    r = eBad;
  }
  else if (feeling == "Ok" || feeling == "ok" || feeling == "OK") {
    r = eOk;
  } 
  else
    return false;
  
  return true;
}

int main() {
    string username;
    cout << "Hello! Please enter your first name here: \n";
    cin >> username;
    cout << "Hello, " << username << "!\n";
    cout << "How are you today? ";
    string feeling;
    cin >> feeling;
    cout << endl;
    
    string_feeling f;
    
    if (! hashit(feeling, f))
      cout << "I do not understand how you are" << endl;
    else {
      switch (f) {
      case eGood:
        cout << "That's great!" << endl;
        break;
      case eBad:
        cout << "I hope you are happy soon!" << endl;
        break;
      case eOk:
        cout << "That's good." << endl;
        break;
      }
    }
}

除此之外:

  • 命名enum
    string\u feeling
    不是很清楚,无论感觉是作为字符串输入的,最好只命名为feeling

  • 在hashit中,可以通过值获取字符串,将其更改为小写,然后将其与“good”、“bad”和“ok”进行比较,或者在其
    .c_str()
    上使用
    strcasecmp
    ,还允许管理“good”等


发布有问题的代码,而不是一些您似乎认为有效但如果我们根据单独给出的一些说明进行编辑会有问题的代码。并删除不需要演示问题的代码。无论是否有
else
,您的程序都有未定义的行为。您需要
在每个可到达的路径中返回
或抛出
。如果不想在
string\u feeling
中添加新的大小写,可以将
hashit
更改为例如
bool hashit(const string&feeling,string\u feeling&v)
在feeling有效时返回false而不设置v,否则设置v并返回trueNote,说明此代码样式的效率。在这种情况下这可能并不重要,但有时应该考虑到这一点。在每个if()语句中进行三次比较,复杂度为O(长度),最坏的情况是函数为O(9*length).Uppercase首先是字符串(可以是“gOOd”?),在最坏的情况下是O(4*length),或者对整个函数进行模式比较,在最坏的情况下是O(length)。“函数返回默认值0”…“但是,默认返回值并不总是0”,这有点矛盾。
pi@raspberrypi:/tmp $ g++ -Wall c.cc
pi@raspberrypi:/tmp $ ./a.out
Hello! Please enter your first name here: 
bruno
Hello, bruno!
How are you today? good

That's great!
pi@raspberrypi:/tmp $ ./a.out
Hello! Please enter your first name here: 
bruno
Hello, bruno!
How are you today? aze

I do not understand how you are
pi@raspberrypi:/tmp $