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

zTB*_*BxN 2 c++ if-statement

在一个简短的基本测试C++程序中,我使if,else if和else语句似乎不起作用.无论你输入if是什么答案,否则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;
Run Code Online (Sandbox Code Playgroud)

}

Jos*_*eld 13

这是因为;之后的else:

else;
Run Code Online (Sandbox Code Playgroud)

这太早结束了陈述,所以下一行与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;
}
Run Code Online (Sandbox Code Playgroud)