我正在尝试开发一个程序来预订酒店房间并收取费用,但我想为人们增加一种在看到价格后拒绝房间的方式。
我尝试了一下,似乎无法找到一个答案,一旦超过接受房费的人,该如何继续下去。
#include <iostream>
using namespace std;
int main()
{
int number_of_rooms{ 0 };
cout << "Welcome to Trey's hotel! How many rooms can we book you for? ";
cin >> number_of_rooms;
cout << "Ok, we will book " << number_of_rooms << " room(s) for you!" << endl;
double price_of_room{ 125.5 };
char decision;
bool run = true;
while (run)
{
cout << "Each room will be " << price_of_room << " per room. Is that Ok with you? ";
cin >> decision;
if (decision == 'N' || decision == 'n' || decision == 'No' || decision == 'no')
cout << "Alright, have a good day! " << endl;
{
run = false;
}
if (decision == 'Y' || decision == 'y' || decision == 'Yes' || decision == 'yes')
{
run = true;
}
}
cout << "Ok for " << number_of_rooms << " that will cost you ";
return 0;
}
答案 0 :(得分:1)
连续三天的编码非常不错!我可以看到两个主要问题:
您需要将cout << "Alright, have a good day!" << endl;
移到其下方的括号中,以便代码显示为:
if (decision == 'N' || decision == 'n' || decision == 'No' || decision == 'no')
{
cout << "Alright, have a good day! " << endl;
run = false;
}
此外,您的decision
声明中有错误。在您的if语句中,您检查决策是否等于“是”,但是,决策是一个字符。为确保if语句正确,您需要在文件顶部添加#include <string>
,然后说string decision;
。