我设法解决了大多数问题。但我仍然有这个问题。当我运行程序时,如果输出/ else。我尝试移动括号,改变if语句和所有内容。到目前为止,我们只研究了循环和if / else语句。我想做一个循环,但我想不出办法。
#include<iostream>
#include <string>
using namespace std;
int main() {
string Cartype, rateoption;
double total, miles, days;
const double CdailyRate=30;
const double PdailyRate=40;
const double FdailyRate=50;
const double CmileRate=0.25;
const double PmileRate=0.35;
const double FmileRate=0.45;
cout<<"Thank you for choosing Car Rite Rental for your rental needs!\n"
<<"\a Before we get started calculating your total owed please remember\n"
<<"that here at Car Rite Rental we havea MINIMUM PAYMENT OF $30.\n\n"
<<"Please enter the type of car you have rented: \n\n"
<<"[please enter corresponding letter] \n"
<<"C-Chevrolet\n"<<"P-Pontiac\n"<<"F-Ford\n";
cin>>Cartype;
cout<<"Please choose your payment option from the following: \n\n"
<<"[please enter corresponding number] \n"
<<"D-Daily Rate\n"<<"M-Mileage Rate\n";
cin>>rateoption;
if(rateoption=="D"||rateoption=="d") {
cout<<"Please enter the number of days you have rented this vehicle: \n";
cin>>days;
} else {
cout<<"Please enter the number of miles traveled in your rental car:\n";
cin>>miles;
}
if (Cartype=="C"||Cartype=="c" && rateoption=="D"||rateoption=="d") {
total=CdailyRate*days;
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
} else if(Cartype=="C"||Cartype=="c" && rateoption=="M"||rateoption=="m") {
total=CmileRate*miles;
if(total>=30)
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
else
cout<<"Your total owed today is: $30.00\n"<<"Thank you again for choosing Car Rite Rental!\n";
}
if (Cartype=="P"||Cartype=="P" && rateoption=="D"||rateoption=="d") {
total=PdailyRate*days;
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
}
if(Cartype=="P"||Cartype=="P" && rateoption=="M"||rateoption=="m") {
total=PmileRate*miles;
if(total>=30)
cout<<"Your total owed today is: $"<<total<<"\nThank you again for choosing Car Rite Rental!\n";
else
cout<<"Your total owed today is: $30.00\n"<<"Thank you again for choosing Car Rite Rental!\n";
}
return 0;
}
答案 0 :(得分:1)
所有if语句中的条件都是错误的。在C ++表达式中,&&
的优先级高于||
,因此您的if语句
if (Cartype=="C"||Cartype=="c" && rateoption=="D"||rateoption=="d")
编译器可以像那样理解
if (Cartype=="C" || (Cartype=="c" && rateoption=="D") || rateoption=="d")
但很明显,你的意思是这样理解
if ((Cartype=="C" || Cartype=="c") && (rateoption=="D" || rateoption=="d"))
因此,请修复所有这样的if语句,看看问题是否消失。
答案 1 :(得分:0)
您需要使用括号对逻辑测试进行分组。
if (Cartype=="C"||Cartype=="c" && rateoption=="D"||rateoption=="d")
我相信你想把比较表达为:
if ((Cartype == "C" || Cartype == "c") && (rateoption == "D" || rateoption == "d"))
&amp;&amp;您编写的运算符不符合您的预期。
另外,请查看函数std::transform
和std::tolower
或std::toupper
以将字符串转换为大写或小写,以便减少表达式的数量。
我建议您使用嵌套 if
语句:
if (Cartype=="C" || Cartype=="c")
{
if (rateoption=="D" || rateoption=="d")
{
// ...
}
if (rateoption == "M" || rateoption=="m")
{
//...
}
}
if (Cartype=="P" || Cartype=="p")
{
if (rateoption=="D" || rateoption=="d")
{
// ...
}
if (rateoption == "M" || rateoption=="m")
{
//...
}
}