2013-10-29 38 views
-2

所以我需要程序執行符合我放置的條件的if語句之一。我使用的輸入是70,15和30,000.它應該進入第二個if語句。這裏是代碼。程序似乎在執行兩個if語句

#include <iostream> 
#include <fstream> 
using namespace std; 
int information1(int hourR); 
int information2(int conTime); 
int information3(int income1); 
int hourR,conTime,income1; 
ofstream outfile; 

int information1(int hourR) 
{ 
    cout<<"Enter hourly rate."; 
    cin>>hourR; 
    return hourR; 
} 

int information2(int conTime) 
{ 
    cout<<"Enter total consulting time in minutes."; 
    cin>>conTime; 
    return conTime; 
} 

int information3(int income1) 
{ 
    cout<<"Enter income."; 
    cin>>income1; 
    return income1; 
} 

int main() 
{ 
    int hours,consultTime,income,fortyPercent,seventyPercent; 
    outfile.open("Billing amount.txt"); 
    hours=information1(hourR); 
    consultTime=information2(conTime); 
    income=information3(income1); 


if(income<=25000) { 
if(consultTime<=30) { 
outfile<<"No service charge."; 
} 
else { 

    fortyPercent=hours*.4*45/60; 
    outfile<<fortyPercent; 
    } 
} 

else 
{ 
if(consultTime<=20){ 

outfile<<"No service charge."; 
} 
else 
{ 
seventyPercent=hours*.7*45/60; 
outfile<<seventyPercent; 
} 
} 
     outfile.close(); 
     return 0; 
    } 

也可以ANY1告訴我,IM是否在做正確的計算?becuz其困惑我,這裏的問題。

在納稅季節期間,每個星期五,J & J會計師事務所爲準備自己的納稅申報表的人員提供幫助 。他們的收費如下:

a。如果一個人收入低(< = 25,000),諮詢時間少於 大於或等於30分鐘,則不收費;否則,服務費用爲30分鐘內正常小時費率的40%。

b。對於其他人,如果諮詢時間少於或等於20分鐘,那麼 不是服務費;否則,在超過20分鐘的時間內,服務費是小時費率的70%。

(例如,假設一個人有收入低和花費1小時15分鐘, 和每小時速度爲$ 70.00,然後將計費量是70.00 * 0.40 *(六十〇分之四十五)= $ 21,00。)

+1

爲什麼你認爲它不會都if語句執行? – drescherjm

+0

改進代碼格式化! –

+0

目前它確實執行,但我需要它執行其中的一個。與我使用的輸入一樣,我在輸出文件中獲得21No服務費用。 – GetYourWeightUp

回答

2

的結構,你現在有這個樣子的:

if(someCondition) { 
    //doStuff 
} else { 
    //do something else 
} 

if(someOtherCondition) { 
    //do stuff 
} else { 
    //do something else 
} 

的代碼會執行任ifelse的第if else對,要麼在ifelse的第二對。

最簡單的解決方案,我能想到看起來像這樣的:

if(income<=25000) { 
    if(consultTime<=30) { 
     //do stuff 
    } else { //this else is paired with if(consultTime<30) and within the if(income<=30) 
     //do stuff when income<=25000 and consultTime>30 
    } 
} else /*income>250000*/ { //this else is paired with if(income<=25000) 
    if(consultTime<=20) { 
     //do stuff 
    } else { //this else is paired with if(consultTime<=20) and within the else paired with if(income<=30) 
     //do stuff when income>25000 and consultTime>20 
    } 
} 
+0

我做了你的改變,但我得到21沒有服務費。你知道21可能來自哪裏嗎? – GetYourWeightUp

+0

將您的更改編輯爲原始問題。不要發表評論。 – nhgrif

+0

@ user2812652 21是希望的答案:hours * .4 * 45/60 when hours = 70.但是,您似乎沒有更改原始代碼以使其在此答案中使用解決方案。 – drescherjm