2016-10-20 2 views
-2

내 C++ 클래스에서 전화 요금 프로그램을 코딩하는 데 문제가 있습니다. 문제는 if, else if 및 else 문이 엉망입니다. 나는 stackoverflow에 관한 다른 프로그램뿐만 아니라 포럼과 내 비슷한 것을 보았지만 나의 오류가 어디에 있는지 이해할 수 없었다. 교수님은 사물을 설명하는 데있어서 가장 위대한 사람은 아니며, 교과서를 통해 지금까지 자신을 가르치고 있습니다. 저는 이것이 아마추어 문제라고 알고 있습니다 만, 여기에 있습니다.if/else if/else 전화 요금 계산기 C++

대화 형 입력, 수학 연산, "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 

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); 
      }  

는 계산을하다 내, 만약 다른 사람과 다른 경우 문이 내 출력에서 ​​제대로 실행됩니다.

+1

"내 출력에서 ​​제대로 작동하려면 if, else if 및 else 문을 어떻게 배열 할 수 있습니까?" ... 그게 무슨 뜻이야 ?? – HazemGomaa

+0

디버거에서 코드를 단계별로 실행하고 결과를 확인하십시오. – MrEricSir

답변

1

봅니다이 한 개있는 경우-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); 
    }  
} 

설명 : 원래 코드는 위 else ifelse 중괄호 내부 if-else 문을 포장하지 않았다.

+3

아마도 크 누스의 사랑 때문에 적절한 코드 형식을 사용하고 있을까요? 초보자 프로그래밍 문제의 90 %는 적절한 가독성 규칙으로 해결할 수 있습니다. –