C++ 为什么两个if语句都出现在输出中。这怎么了
我设法解决了大部分问题。但我仍然有这个问题。当我运行程序时,if/else都被输出。我试着移动括号,改变if语句和所有内容。到目前为止,我们只研究了循环和if/else语句。我想做一个循环,但我想不出做循环的方法C++ 为什么两个if语句都出现在输出中。这怎么了,c++,loops,cin,C++,Loops,Cin,我设法解决了大部分问题。但我仍然有这个问题。当我运行程序时,if/else都被输出。我试着移动括号,改变if语句和所有内容。到目前为止,我们只研究了循环和if/else语句。我想做一个循环,但我想不出做循环的方法 #include<iostream> #include <string> using namespace std; int main() { string Cartype, rateoption; double total, miles, d
#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;
}
#包括
#包括
使用名称空间std;
int main(){
字符串类型,rateoption;
双倍的总里程、天数;
常数双CdailyRate=30;
常数双PdailyRate=40;
const-double-FdailyRate=50;
常数双CmileRate=0.25;
常数双PmileRate=0.35;
常数双FmileRate=0.45;
CUT< P>所有IF语句中的条件都是错误的。在C++表达式中,代码>和< /代码>比 > < < /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 (Cartype=="C" || (Cartype=="c" && rateoption=="D") || rateoption=="d")
if ((Cartype=="C" || Cartype=="c") && (rateoption=="D" || rateoption=="d"))
所以,像这样修复所有的if语句,看看问题是否消失。您需要使用括号对逻辑测试进行分组
if (Cartype=="C"||Cartype=="c" && rateoption=="D"||rateoption=="d")
我相信你想将这种比较表述为:
if ((Cartype == "C" || Cartype == "c") && (rateoption == "D" || rateoption == "d"))
您编写的&&运算符的行为与预期不符
另外,查看函数std::transform
和std::tolower
或std::toupper
将字符串转换为大写或小写,以便减少表达式的数量
编辑1:嵌套的if
我建议您使用嵌套的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")
{
//...
}
}
保证您一直在启用并注意所有编译器警告?有很多if…else语句。您具体指的是哪一个?顺便说一句,您所有if语句中的布尔逻辑都是错误的if(Cartype==“C”| Cartype==“C”&&ratepoption==“D”| ratepoption==“D”)
应该是if((Cartype==“C”||Cartype==“c”)&(rateoption==“D”| | rateoption==“D”)
等。您的格式还有很多需要改进的地方。如果您正确缩进代码,这样的事情会更容易处理。@BobbyDigital是对的:这次我为您解决了这个问题,但总是提供格式正确的代码。我们是来帮助您的,所以请善待我们的眼睛和心灵。