嵌套 if 检查 C++
Nesting if checks with C++
也许我的代码很草率,但出于某种原因,它只是通过了所有嵌套的 If 检查,而没有停下来检查第一个以外的答案。此外,无论我给它一个 False 答案如何,第一个 If 检查都会继续检查嵌套的 If for True。我是 C++ 的新手,所以我缺少什么吗?
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<" \n";
cout<<"UUU UUU RRRRRRR TTTTTTTTT HHH HHH\n";
cout<<"UUU UUU RRR RRR TTT HHH HHH\n";
cout<<"UUU UUU RRRRRRR TTT HHHHHHHH\n";
cout<<"UUU UUU RRR RRR TTT HHH HHH\n";
cout<<" UUUUUUU RRR RRR TTT HHH HHH\n";
cout<<" \n";
cout<<" Created by: Illyduss\n";
cout<<" 2015\n";
cout<<" \n";
cout<<" \n";
cout<<" \n";
int account;
cout<<"Do you already have an account? \n";
cout<<"Type 'New' to create an account or enter your account name.\n";
cout<<"Account Name: ";
cin>> account;
cin.ignore();
if (account = "New" or "NEW" or "new"){
string surname;
cout<<" \n";
cout<<"Account names serve as the Surname or Last name for\n";
cout<<"all characters linked to said account. This is beca\n";
cout<<"use of our unique gene pool system. Which will be c\n";
cout<<"overed more in depth later on, but for now just thi\n";
cout<<"nk of it like this, an account is a family tree for\n";
cout<<"all of your characters.\n";
cout<<" \n";
cout<<"Please enter your desired Surname Name: ";
cin>> surname;
cin.ignore();
if (surname.length() > 2){
cout<<" \n";
cout<<"You have chosen, '" << surname << "' as your surname, correct? ";
}
else {
cout<<"That is too short, please choose another surname: ";
}
}
else {
cout<< "Welcome back, '" << account << "'!\n";
cout<<"Please enter your password: ";
}
cin.get();
}
首先,您尝试使用 int
类型的对象来输入字符串,然后您尝试使用不正确的条件和赋值运算符 [=16] 将该对象与字符串文字进行比较=]
if (account = "New" or "NEW" or "new"){
^^
您应该将 account
定义为 std::string
类型,在这种情况下,条件可能类似于
if (account == "New" or account == "NEW" or account == "new"){
但无论如何最好将使用某些中间对象的 account
转换为大写。在这种情况下,条件看起来会更简单。
也许我的代码很草率,但出于某种原因,它只是通过了所有嵌套的 If 检查,而没有停下来检查第一个以外的答案。此外,无论我给它一个 False 答案如何,第一个 If 检查都会继续检查嵌套的 If for True。我是 C++ 的新手,所以我缺少什么吗?
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout<<" \n";
cout<<"UUU UUU RRRRRRR TTTTTTTTT HHH HHH\n";
cout<<"UUU UUU RRR RRR TTT HHH HHH\n";
cout<<"UUU UUU RRRRRRR TTT HHHHHHHH\n";
cout<<"UUU UUU RRR RRR TTT HHH HHH\n";
cout<<" UUUUUUU RRR RRR TTT HHH HHH\n";
cout<<" \n";
cout<<" Created by: Illyduss\n";
cout<<" 2015\n";
cout<<" \n";
cout<<" \n";
cout<<" \n";
int account;
cout<<"Do you already have an account? \n";
cout<<"Type 'New' to create an account or enter your account name.\n";
cout<<"Account Name: ";
cin>> account;
cin.ignore();
if (account = "New" or "NEW" or "new"){
string surname;
cout<<" \n";
cout<<"Account names serve as the Surname or Last name for\n";
cout<<"all characters linked to said account. This is beca\n";
cout<<"use of our unique gene pool system. Which will be c\n";
cout<<"overed more in depth later on, but for now just thi\n";
cout<<"nk of it like this, an account is a family tree for\n";
cout<<"all of your characters.\n";
cout<<" \n";
cout<<"Please enter your desired Surname Name: ";
cin>> surname;
cin.ignore();
if (surname.length() > 2){
cout<<" \n";
cout<<"You have chosen, '" << surname << "' as your surname, correct? ";
}
else {
cout<<"That is too short, please choose another surname: ";
}
}
else {
cout<< "Welcome back, '" << account << "'!\n";
cout<<"Please enter your password: ";
}
cin.get();
}
首先,您尝试使用 int
类型的对象来输入字符串,然后您尝试使用不正确的条件和赋值运算符 [=16] 将该对象与字符串文字进行比较=]
if (account = "New" or "NEW" or "new"){
^^
您应该将 account
定义为 std::string
类型,在这种情况下,条件可能类似于
if (account == "New" or account == "NEW" or account == "new"){
但无论如何最好将使用某些中间对象的 account
转换为大写。在这种情况下,条件看起来会更简单。