-2

So I need the program to execute either one of the if statements that meet the conditions I've put. The input I used was 70, 15, and 30,000.It should go into the second if statement.Here is the code.

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

Also can any1 tell me whether im doing the calculations right?becuz its confusing me,here is the problem.

During the tax season, every Friday, the J&J accounting firm provides assistance to people who prepare their own tax returns. Their charges are as follows:

a. If a person has low income (<=25,000) and the consulting time is less than or equal to 30 minutes, there are no charges; otherwise, the service charges are 40% of the regular hourly rate for the time over 30 minutes.

b. For others, if the consulting time is less than or equal to 20 minutes, there are no service charges; otherwise, service charges are 70% of the regular hourly rate for the time over 20 minutes.

(For example, suppose that a person has low income and spent 1 hour and 15 minutes, and the hourly rate is $70.00. Then the billing amount is 70.00*0.40*(45 / 60)= $21.00.)

4

1 回答 1

2

The structure you currently have looks like this:

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

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

The code will go execute either the if or the else for the FIRST if else pair AND either the if or the else for the SECOND pair.

The easiest solution I can think of looks like this:

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
    }
}
于 2013-10-29T01:23:14.720 回答