我在c ++课程中编写电话帐单程序时遇到问题。问题出在我的if,else if和else语句的混乱中。我查看了论坛以及与我类似的stackoverflow上的其他程序,但无法理解我的错误在哪里。我的教授在解释事情方面不是最好的,到目前为止,我一直在教自己。我知道这是一个非常业余的问题,但现在就是这样。
该计划的目的是练习交互式输入,数学运算,“if ... else”和“if else if”链。
系统会要求您输入电话号码(1-3)。电话计划选项如下..
Available Phone Plans:
1. Ultimate Unlimited talk and data Unlimited Text $75.00
2. Deluxe Unlimited talk, 3 GB data 550 Text messages $55.00
3. Basic Unlimited talk, 1 GB data 250 Text messages $35.00
对于计划2和3,每增加一条短信将收取0.20美元(20美分)的费用。
这些是我制造的变量..
string customerName, phoneNumber, planType;
int planChoice, textMessages;
float planCost, overTextTotal, untaxedTotal, taxedTotalDeduction,
taxedTotalBill, stateFeeDeduction, federalFeeDeduction;
float overTextFee = .20;
const float STATEFEE = .05;
const float FEDERALFEE = .02;
这些是计算 //计算
untaxedTotal = planCost + overTextTotal;
stateFeeDeduction = untaxedTotal * STATEFEE;
federalFeeDeduction = untaxedTotal * FEDERALFEE;
taxedTotalBill = untaxedTotal + stateFeeDeduction + federalFeeDeduction;
cout<<"\n\tEnter 1 - 3 For Plan Type: ";
cin >> planChoice;
cout<<"\nEnter Number Of Text Messages Used For The Month: ";
cin >> textMessages;
if(planChoice == 1 )
{
planType = "Ultimate";
planCost = 75.00;
overTextTotal = 0;
}
else if(planChoice == 2)
{
planType = "Deluxe";
}
if( textMessages <= 550)
{
planType = "Deluxe";
planCost = 55.00;
}
else
{
planCost = 55.00;
overTextTotal = ((textMessages - 550 ) * overTextFee);
}
if (planChoice == 3)
{
planType = "Basic";
}
if (textMessages <= 250)
{
planCost = 35.00;
}
else
{
planCost = 35.00;
overTextTotal = ((textMessages - 250 ) * overTextFee);
}
我的问题是如何安排我的if,else if和else语句在我的输出中正确运行。
答案 0 :(得分:1)
尝试使用以下内容更改if-else语句:
if(planChoice == 1 )
{
planType = "Ultimate";
planCost = 75.00;
overTextTotal = 0;
}
else if(planChoice == 2)
{
planType = "Deluxe";
if( textMessages <= 550)
{
planType = "Deluxe";
planCost = 55.00;
}
else
{
planCost = 55.00;
overTextTotal = ((textMessages - 550 ) * overTextFee);
}
}
else if (planChoice == 3)
{
planType = "Basic";
if (textMessages <= 250)
{
planCost = 35.00;
}
else
{
planCost = 35.00;
overTextTotal = ((textMessages - 250 ) * overTextFee);
}
}
说明:您的原始代码未将内部if-else
语句包装在上部else if
和else
花括号中。