To write a C++ program that:

 Builds upon and reinforces our work in Project #1

 Utilizes selection (if, if..else) and iteration (while, for, do..while) constructs, as

appropriate

 Illustrates multi-way branches

Prep Readings:

Problem Solving with C++, Chapters 2 and 3.

Project Requirements:

1. Develop a program that calculates the cost of a mobile device service. The cost of the
service is determined by the base price and number of message units to be used. The
packages are structured so that the basic package, package "A," is the least expensive if the
user doesn't use many message units, however if the user uses more than the pre-paid
minimum, the charge is significantly more for each additional message unit. Users who
choose package "C" pay a flat rate for unlimited use. The cost of Package "B" is between
these extremes. Details of the packages structure are listed here:

Package Base Price Additional Fees

A $19.99 $0.25 per message unit after 20 units
B $39.99 $0.10 per message unit after 100 units
C $59.99 Unlimited message units

Respuesta :

Answer:

#include<iostream>

using namespace std;

//function to calculate cost of package A

float calculateA(int u){

  float value=19.99;

  if(u>20)

      value=value+(u-20)*0.25;

  return value;

}

//function to calculate cost of package A

float calculateB(int u){

  float value=39.99;

  if(u>100)

      value=value+(u-100)*0.10;

  return value;

}

void calculate(char p,int u){

  float result;

 

  //if package is A call the calculateA function to get the result

  //if package is B call caculateB function to get the result

  //if package is c,then set result=59.99

  if(p=='A' ||p=='a'){

      result=calculateA(u);

      cout<<"The charges are $"<<result<<"\n";

      if(calculateB(u)<result)

          cout<<"By switching to Package B you would save $"<<result-calculateB(u)<<"\n";

      if(59.99<result)

          cout<<"By switching to Package C you would save $"<<result-59.99<<"\n";

     

     

  }else if(p=='B'|| p=='b'){

      result=calculateB(u);

      cout<<"The charges are $"<<result<<"\n";

      if(59.99<result)

          cout<<"By switching to Package C you would save $"<<result-59.99<<"\n";

  }else{

      result=59.99;

      cout<<"The charges are $"<<result<<"\n";

  }

 

}

//main method

int main(){

  char choice;

  while(1){

      char package;

      int units;

      //loop to get package name

      while(1){

          cout<<"Which package are you shopping for? (enter A, B, C)? ";

          cin>>package;

          if(package=='A' || package=='a' || package=='B' || package=='b' || package=='C' ||package=='c')

              break;

          cout<<"\nEnter only A, B, or C.\n";

      }

      //loop to get no of units

      while(1){

          cout<<"How many message units(1 - 672)? ";

          cin>>units;

          if(units>=1 && units<=672)

              break;

          cout<<"\nPlease enter a number between 1 and 672 inclusive\n";

      }

      //caling funtion to calculate cost

      calculate(package,units);

      cout<<"\nDo you want to go again (y/n)? ";

      cin>>choice;

      if(choice=='N' || choice=='n')

          break;

  }

  cout<<"\nThank you for using this program. Goodbye.";

  return 0;

}

Explanation:

ACCESS MORE