C ++ if,else if和else语句问题

zTBxN

在简短而基本的C ++程序测试中,我制作了if,else if和else语句似乎不起作用。不管输入什么答案,如果输入if,else,if和else语句,都会自动跳转到else语句,而我不明白为什么。我尝试了许多不同类型的方法来消除此错误,但是似乎都没有用

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
string name;
string feeling;
int age;

//IMPORTANT MESSAGE
cout << "THIS PROGRAM IS CAPS/SLANG SENSITIVE!" << endl << endl;
//IMPORTANT MESSAGE

cout << "Hello User, please enter name: ";
cin >> name;
cout << endl << endl;

cout << "Now please enter your age: ";
cin >> age;
cout << endl << endl;

cout << "Hello " << name << ", you are " << age << " years old." << endl << endl;
system("pause");
cout << endl;

cout << "How are you today " << name << "?: ";
cin >> feeling;
cout << endl << endl;

if (feeling == "Good")
cout << "That's great!";

else if (feeling == "Okay")
cout << "Fair enough.";

else;
cout << "Well to be fair I don't care so good day :)." << endl << endl;

cin.get();
return 0;

}

约瑟夫·曼斯菲尔德

这是因为;之后的else

else;

这太早地结束了该语句,因此下一行与if条件无关

您应该养成在if上放花括号的习惯

if (feeling == "Good") {
  cout << "That's great!";
} else if (feeling == "Okay") {
  cout << "Fair enough.";
} else {
  cout << "Well to be fair I don't care so good day :)." << endl << endl;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章