1 Replies - 375 Views - Last Post: 06 December 2008 - 02:09 PM Rate Topic: -----

#1 ADJR7621  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-November 08

Functions

Posted 06 December 2008 - 02:05 PM

This is for a class,

this is what the teacher is asking for
At minimum, the program should implement three separate functions (in addition to main). Minimally, one function should get input, one function should calculate the amount of the bill, and one function should produce the bill output. Additionally, at least one of these functions must pass a parameter by reference (where it makes sense to do so).

How do I return both the code and the minutes to the main function. Then how do I tell the next two user defined functions to look into the main function for the needed variables? The total_Bill function will need to look for both the minutes and the code variables. While the output will need to look for code, minutes and bill.

Thanks for any help

//------------------------------------------------------------------------
// This program estimates a monthly phone bill
//------------------------------------------------------------------------
# include <iostream>  
# include <iomanip>  
# include <cmath>  
# include <cctype>
  
using namespace std; 

char get_Code(){ // user defined function

  char code = ' ';
  // menue of different phone plans offered. 
  cout << "Code F: "<<"$150.00 unlimited monthly plan (No Businesses)." << endl;
  cout << "Code M: " << "$5.00 monthly fee, plus 21 cents per min." << endl;
  cout << "Code H: " << "$40.00 for first 600 min. 19 cents per min. after";
  cout << "first 600 min." << endl;
  cout << "Code P: " << "$64.00 for first 1000 min. 17 cents per min. after";
  cout << "first 1000 minutes." << endl;
  cout << "Code B: " << "$150.00 for up to 2000 min; $210.00 for up to";
  cout << " 3000 min;" << endl;
  cout << "$240.00 if over 3000 min." << endl;
  cout << endl << endl;
  
  cout << "Please enter valid code for you phone plan" << endl;
  cin >> code;  // stores entry for user's choice
  code = toupper (code);
  
  // ensures that the user does enter a valid code
  while ((code != 'F')&&(code != 'M')&&(code != 'H')&&(code != 'P')&&(code !='B')){
  cout << "Error" << endl;
  cout << "Please enter valid code for your phone plan" << endl;
  cin >> code; 
  code = toupper (code);
  }
  
  if (code != 'F'){	
	  cout << "Please enter the number of minutes used" << endl;
	  cin >> minutes; // stores entry for user's minutes
  }
  
  // ensures user does not enter a negative amount.
  if (minutes < 0){
  cout << "Error" << endl;
  cout << "Please enter a positive number" << endl;
  cin >> minutes;
  }
  
  return code;
}

float total_Bill(){ // user defined function 

  if (code == 'F'){ // since plan F is unlimited sets total bill to $150.00
	  bill = 150.00;
  }
  
  if (code == 'M'){ // plan M charges 5.00 monthly fee and 0.21 per minute.
	  bill = minutes * 0.21 + 5.00;
  }
  
  if (code =='H'){
	  minutes = minutes - 600; // calculates minutes that user enters
	  
	  if (minutes < 600){
		  minutes = 0.0; // if minutes are less than 600, keeps the program  
						 // from calculating a credit.
	  bill = (minutes * 0.19) + 40.00; // calculates monthly phone bill. 
  }
}	
  
  if (code == 'P'){
	  minutes = minutes - 1000; // calculates minutes that user enters
	  
	  if (minutes <1000){  // if minutes are less than 600, keeps the program  
		 minutes = 0.0;   // from calculating a credit.
		 bill = minutes * 0.17 + 64.00;  // calculates monthly phone bill.   
	  } 
}
  
  if ((code == 'B')&&(minutes <= 2000)){
	   bill = 150.00; // plan B is a set 150.00 bill for less than 2000 
  }						 // minutes.
  
  if ((code == 'B')&&(minutes > 2000)&&(minutes <= 3000)){
	   bill = 210.00; // plan B is a set 210.00 bill for less than 3000 min.
  }
  if ((code == 'B')&&(minutes > 3000)){
	   bill = 240.00; // plan B is a set 240.00 bill for anything over 3000
  }						 // minutes. 

  return bill;
}  

void Output (){ // Outputs the following:  

  cout << "Your phone plan is, plan: " << code << endl;
  cout << "Minutes used: " << minutes << endl;
  cout << "Your phone bill is: " << bill << endl;
  return;
}
  
int main () {  
  
  float total_Bill = 0.0;
  float minutes = 0.0;
  float bill = 0.0;
  char code = ' ';
  
  cout <<"-----------------------------------------------------------"<< endl;
  cout << "This program calculates & displays";
  cout << " your monthly phone bill." << endl;
  cout <<"-----------------------------------------------------------"<< endl;
  cout << endl << endl;

  code = get_Code();
  
  bill = total_Bill(); 
  
  Output();
   
  cout << endl;  
  system("pause");  
  return 0;	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Functions

#2 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: Functions

Posted 06 December 2008 - 02:09 PM

use structs and classes to return multiple types. The easiest way for this to work would be to implement a customer class. Simple, no inheritance/polymorphism required. Make those three functions either part of or friends of the customer class, and if they're friends make sure you pass an object.

You're biggest error here is scoping. All of the functions reference "code" when it is not a global variable.

This post has been edited by polymath: 06 December 2008 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1