if/else if/else电话账单计算器

if/else if/ else phone bill calculator C++

本文关键字:if else 计算器 电话 账单      更新时间:2023-10-16

我在c++类中编写电话账单程序时遇到了麻烦。问题出在if, else if和else语句的混乱中。我通过论坛以及stackoverflow上类似于我的其他程序,但无法理解我的错误在哪里。我的教授不擅长解释事情,到目前为止,我都是通过课本自学的。我知道这是一个非常业余的问题,但这就是它。

程序的目的是练习交互式输入、数学运算、"if…else"answers"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<<"ntEnter 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语句在我的输出中正确运行。

试着用下面的语句改变你的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 ifelse花括号中。