为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
Why does this case statement need an "if else" and not just an "if"?
所以我是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
得到不同的结果。
- 为什么这个案例陈述需要一个"if else"而不仅仅是一个"if"?
- 检查一个类型是否直接派生自"enable if"上下文中的另一个类型(是其子类型)
- 而不是那么多的 if 语句,我想要一个逻辑,我可以用一个语句或优化的方式来完成
- 尝试使用 indexOf 创建一个 if 语句来检查字符串是否包含字符.有一点麻烦
- 我如何在一个 if 语句中声明所有数字我尝试通过其他方式声明所有数字,如果一个接一个,但似乎代码有逻辑错误
- 为什么我的代码在第一个 if 语句处中断?
- 为什么我要尝试在一个if语句中调用两个fucntions,但是在第一个完成后它忽略了另一个功能?(C )
- 输入后,我的程序似乎跳过了第一个IF语句,直接转到ELSE
- 从一个 if 语句转到另一个语句
- 程序仅执行第一个 IF 语句
- C++程序仅执行第一个"if"语句
- ifs 是否按照它们的编写顺序进行测试是一个 if-else..如果-否则..如果-否则块
- C++ 入门 1.4.4.不太理解第一个IF语句的必要性
- 我写了一个if/elseif语句,但它只给了我第一个答案,为什么
- 为什么无论输入如何,我的第一个 "if" 语句都会执行?
- 为什么我的第一个 if 语句有效,但我的 else if 语句在它之后不起作用
- 是一个if语句,保证不会被过多地求值
- 你好!我是新的c++和我试图使一个脚本,将打印一个if语句到一个文件
- Visual c++编译器忽略第一个if语句
- 我如何设置一个if语句来动画一个精灵