[C ++]程序无需解释即可终止

时间:2016-04-07 22:23:06

标签: c++

int main()
{
cout << "Welcome to your personal seat booking program." << endl;
cout << "Please follow the instructions to book your seat." << endl << endl;
cout << "Would you like a seat by the window? (true/false)" << endl;
cin >> userInputWindow;
if (userInputWindow = true)
{

}
else if (userInputWindow = false)
{

}

cout << "Would you like a seat with a table? (true/false)" << endl;
cin >> userInputTable;
if (userInputTable = true)
{

}
else if (userInputTable = false)
{

}

cout << "Would you like to be seated in First Class? (true/false)" << endl;
cin >> userInputClass;
if (userInputClass = true)
{

}
else if (userInputClass = false)
{

}

cout << "Do you need Ease-Of-Access seating? (true/false)" << endl;
cin >> userInputAccess;
if (userInputAccess = true)
{

}
else if (userInputAccess = false)
{

}

cout << "Do you need a forward facing seat? (true/false)" << endl;
cin >> userInputForward;
if (userInputForward = true)
{

}
else if (userInputForward = false)
{

}

当我运行它时,它会要求我输入第一个cin语句,但是跳过脚本的其余部分,甚至跳过我最后的system("PAUSE")。有什么理由吗?我很茫然,但我对编程非常陌生,并且遇到了困难。 谢谢!

1 个答案:

答案 0 :(得分:0)

发生的事情是,在第一次选择if和else if,因为if if和if else内部没有任何内容,程序将终止,无论用户输入true还是false。什么选择if和else if是否就像拦截一样,你是左转还是右转(选择转到第一个if,或者第二个if else),并继续前进直到道路结束(程序终止)。因此,当程序结束时没有解释,因为没有需要解释的语法错误。 我假设您希望程序在用户输入“true”时提出问题。

http://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm

int main()
{
cout << "Welcome to your personal seat booking program." << endl;
cout << "Please follow the instructions to book your seat." << endl << endl;
cout << "Would you like a seat by the window? (true/false)" << endl;
cin >> userInputWindow;
if (userInputWindow == true)
{

cout << "Would you like a seat with a table? (true/false)" << endl;
cin >> userInputTable;
if (userInputTable == true)
{
cout << "Would you like to be seated in First Class? (true/false)" << endl;
cin >> userInputClass;
if (userInputClass == true)
{
cout << "Do you need Ease-Of-Access seating? (true/false)" << endl;
cin >> userInputAccess;
if (userInputAccess == true)
{   
cout << "Do you need a forward facing seat? (true/false)" << endl;
cin >> userInputForward;
if (userInputForward == true)
{
cout << "Program terminated";
}
else if (userInputForward == false)
{
}
}
else if (userInputAccess == false)
{
}
}
else if (userInputClass == false)
{

}
}
else if (userInputTable == false)
{

}
}
else if (userInputWindow == false)
{

}

如果您继续输入true,这将使程序继续运行,不知道这是否是您正在寻找的。