我正在尝试做一个项目,询问有关酒店访问的问题,并重复回答问题,非常简单。到目前为止,我有这个代码:这段代码为什么跳过设置字符串变量的if语句?
#include<iostream>
#include<string>
using namespace std;
int main()
{
int df;
int days;
string bed;
string bn;
cout << "Welcome to C++ Hotel, we have to ask you a few questions about your stay in order to give you the correct rate." << endl;
cout << "First, what floor would you like to stay on? (We currently have rooms available from floors 2 to 12)" << endl;
cin >> df;
while (df > 12 && 2 < df){
cout << "Sorry, but your answer is invalid, please enter a floor from 2 to 12." << endl;
cin >> df;
}
cout << "Okay, we will register your room on floor " << df << "." << endl;
cout << "Now, what type of bed will you be requesting during your visit? On floor " << df << " we currently have doubles, queens, or a suite." << endl;
cin >> bed;
while (bed != "d" && bed != "q" && bed != "s"){
cout << "Sorry, but your answer is invalid, please choose between a double, queen or suite by entering either d, q, or s respectively." << endl;
cin >> bed;
}
if (bed == "d"){
string bn = "double";
}
if (bed == "q"){
string bn = "queen";
}
if (bed == "s"){
string bn = "suite";
}
cout << "Okay, your room will be on floor " << df << " with a " << bn << " sized bed!" << endl;
}
我希望用户能够输入d,q或S为双,女王或套房床尺寸的选择,但我也不想重复他们选择了什么在问题的最后,如果它表达了“你已经选择了d尺寸的床!”的话,这显然听起来很愚蠢!
因此,我做了几个if语句,基本上根据用户输入的原始床变量的基础设置了一个新的字符串变量。所以如果他们把double放在d中,那么bn变量被设置为“double”,然后在最后的cout中调用bn。
但是,代码似乎只是完全跳过了3个if语句,然后在变量被调用时它只是空白,代码运行良好,没有任何错误或警告或任何事情,但我可以'弄清楚为什么它只是不承认代码,对我来说似乎没有问题?
'df> 12 && 2 12 || df <2' –
@Dante:如果您怀疑程序正在跳过代码,那么您应该在其中放置一个'cout'语句来打印某些内容(或者甚至更好地使用'cerr',或者甚至更好地使用调试器并逐步执行代码)。 – indiv
'df> 12 && 2