为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?

Why does this case statement need an "if else" and not just an "if"?

本文关键字:一个 if 不仅仅是 else 陈述需 案例 为什么      更新时间:2023-10-16

所以我是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.";
}
}

每当我在"if(感觉==ok("之后没有"else"时,默认大小写永远不会被调用,如果我输入一些随机的东西,它会给我来自eGood案例的文本。我想知道为什么会发生这种情况,因为我正在学习C++我不想只是忽略它,而不知道为什么在我把 else 语句放在那里后它会起作用。所以,如果有人能向我解释这一点,那就太好了!对不起,我的语法不好。

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

在未启用警告的情况下编译代码是浪费时间的可靠方法。

当函数中三个if语句中的任何条件都不为真时hashit函数中不执行return语句并调用未定义的行为

(引自N3337 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
}

你总是必须返回一个值,否则行为是未定义的

如果您无法修改枚举以添加未知感觉的情况,则可以修改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;
}
}
}

编译和执行:

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 $ 
<小时 />

其中:

  • 命名你的枚举string_feeling不是很清楚,无论感觉是作为字符串输入的,最好只是将其命名为感觉

  • hashit中,按值获取字符串以将其更改为小写,然后将其与"好"坏"和"好"进行比较,或者在其.c_str()上使用strcasecmp,允许也管理"gOoD"等

如果没有满足if条件,hashit去了

else cout << "";

由于您没有显式编写return语句,因此该函数返回默认值0,等于eGood

但是,默认返回值并不总是0。这是一个undefined behaviour.

如果使用其他编译器运行此代码,则may得到不同的结果。

相关文章: