我试图制作一个项目,询问有关酒店访问的一些问题并重复回答,非常简单。到目前为止,我有这个代码:
#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,但我也想重复他们在问题末尾所选择的内容,这显然听起来很愚蠢如果它说的是&#34;你已经选择了d尺寸的床!&#34;
所以我做了一些if语句,基本上根据用户输入的原始床变量设置了一个新的字符串变量。因此,如果他们输入d为double,则bn变量设置为&#34; double&#34;,然后在最后cout中调用bn。
然而,代码似乎只是完全跳过了3个if语句,并且当调用变量时它只是空白,代码运行良好,没有错误或警告或任何东西,但我可以&#39 ;弄清楚为什么它不能识别代码,对我来说似乎没问题?
答案 0 :(得分:5)
将string bn = "double";
替换为
bn = "double";
您要声明另一个名为string
的本地bn
并将其设置为某个值,并且您要设置的实际bn
保持不变。
进一步阅读:Scope in C++