我正在嘗試做一個項目,詢問有關酒店訪問的問題,並重復回答問題,非常簡單。到目前爲止,我有這個代碼:這段代碼爲什麼跳過設置字符串變量的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