We are just learning user defined functions. Instructor has us taking a prev assignment and pulling the calculations into functions. I thought I understood how to do the function, but guess not I''m getting the "error term does not evaluate to a function taking 0 arguments." I have been working on this for 3 days. I've looked to make that the function isn't named the same as a variable. Can someone be kind enough to nudge me in the correct direction? Thank you for your time.

#include <iostream>
#include <iomanip>

using namespace std;

      //Named constants – Regular Service
const double REG_BASIC_SERV_COST = 10;
const double REG_OVER_MIN_CHRG = .20;
const double REG_MINUTES_FREE = 50;

      //Named constants – Premium Serivce
const double PREM_BASIC_SERV_COST = 25.00;
const double PREM_DAY_FREE_MIN = 75;
const double PREM_NIGHT_FREE_MIN = 100;
const double PREM_DAY_OVER_CHRG = .10;
const double PREM_NIGHT_OVER_CHRG = .05;
const double PREM_OVER_MIN = .10;

double RegularBill;
double PremiumBill;


int main()
{
        //Variable declaration
    int AccountNumber; 
    char ServiceType; 
    int RegServiceMinutes;
    int PremDayMinutes;
    int PremNightMinutes;
    double AmountDue;

    cout << fixed << showpoint;                     
    cout << setprecision(2);                      

    cout << "This program calculates and prints a cellular phone bill" << endl;
    cout << endl;

    cout << "Enter account number: " ;  
    cin >> AccountNumber;                           
    cout << endl;

    cout << "Enter Service type: " 
         << "R or r (Regular Service), "
         << "P or p (Premium Service):  ";               
    cin >>  ServiceType;  
	cout << endl;

	switch (ServiceType)
    {
    case 'r':                                       
    case 'R': 
AmountDue = RegularBill();
    cout << "Account Number: " << AccountNumber << endl;
    cout << "Type of Service:  " << ServiceType << endl;
    cout << "Amount Due: $" << bAmount << endl; 
break;
    case 'p':                                       
    case 'P': 
AmountDue = PremiumBill();
    cout << "Account Number: " << AccountNumber << endl;
    cout << "Type of Service:  " << ServiceType << endl;
    cout << "Amount Due: $" << bAmount << endl; 
break;

       default: 
    cout << "Invalid customer type." << endl;
    cout << "Program terminates" << endl;
	}                                           //end switch

  system ("pause");
  return 0;
}  

double RegularBill()

double bAmount;
{
    cout << "Enter number of minutes used for Regular Service:  ";
    cin  >> RegServiceMinutes;
    cout << endl;

    if (RegServiceMinutes <= REG_MINUTES_FREE)
    {
        bAmount = REG_BASIC_SERV_COST << endl;
        else 		
	bAmount = REG_BASIC_SERV_COST +(RegServiceMinutes - REG_MINUTES_FREE)
 * REG_OVER_MIN_CHRG;
		}
return bAmount;
}

double PremiumBill()
{
double bAmount;
//double AmountDue;

    cout << "Enter the number minutes used during the day:  ";
    cin  >> PremDayMinutes;
    cout << endl;
    cout << "Enter the number of minutes used during the          evening:  ";
    cin >> PremNightMinutes;
    cout << endl;

    bAmount = PREM_BASIC_SERV_COST;
    if (PremDayMinutes > PREM_DAY_FREE_MIN)
    {			
     bAmount = bAmount +(PremDayMinutes -  PREM_DAY_FREE_MIN)
* PREM_DAY_OVER_CHRG;
	 
     if (PremNightMinutes > PREM_NIGHT_FREE_MIN)
     bAmount = bAmount + (PremNightMinutes - PREM_NIGHT_FREE_MIN) 
* PREM_NIGHT_OVER_CHRG;
          else
	bAmount = PREM_BASIC_SERV_COST;
return bAmount;
}

I fixed the error. Now I get an error saying "using bAmount before initializing". Its not returning the amount from the function. Can you explain how a user defined function is supposed to be created? I didn't think it was that difficult and I've reread everything in the book. Very simplified create function name and put the prototype before int main() create the stmts that will accept the returned amount from the function, then create the function itself using the prototype name, return the calculation amt to the the calling stmt.
How far off am I?

double RegularBill();
double PremiumBill();


int main()
{
        //Variable declaration
    int AccountNumber; 
    char ServiceType; 
//	int RegServiceMinutes;
//	int PremDayMinutes;
//	int PremNightMinutes;
    double AmountDue;
    double bAmount;

    cout << fixed << showpoint;                     
    cout << setprecision(2);                      

    cout << "This program calculates and prints a cellular phone bill" << endl;
    cout << endl;

    cout << "Enter account number: " ;  
    cin >> AccountNumber;                           
    cout << endl;

    cout << "Enter Service type: " 
	<< "R or r (Regular Service): "
         << "P or p (Premium Service):  ";               
    cin >>  ServiceType;  
    cout << endl;

switch (ServiceType)
    {
    case 'r':                                       
    case 'R': 
    AmountDue = RegularBill();

    cout << "Account Number: " << AccountNumber << endl;
    cout << "Type of Service:  " << ServiceType << endl;
    cout << "Amount Due: $" << bAmount << endl; 
break;
 
    case 'p':                                       
    case 'P': 
    bAmount = PremiumBill();

    cout << "Account Number: " << AccountNumber << endl;
    cout << "Type of Service:  " << ServiceType << endl;
    cout << "Amount Due: $" << bAmount << endl; 
break;

       default: 
     cout << "Invalid customer type." << endl;
     cout << "Program terminates" << endl;
	}                  //end switch

  system ("pause");
  return 0;
}  //end int

double RegularBill()
{
double bAmount;
int RegServiceMinutes;

    cout << "Enter number of minutes used for Regular Service:  ";
    cin  >> RegServiceMinutes;
    cout << endl;

    if (RegServiceMinutes <= REG_MINUTES_FREE)
//{
     bAmount = REG_BASIC_SERV_COST;

      else 		
        bAmount = REG_BASIC_SERV_COST +(RegServiceMinutes - REG_MINUTES_FREE)
					* REG_OVER_MIN_CHRG;
//}
return bAmount;
}

double PremiumBill()
{
    double bAmount;
    int PremDayMinutes;
    int PremNightMinutes;
    double AmountDue;

    cout << "Enter the number minutes used during the day:  ";
    cin  >> PremDayMinutes;
    cout << endl;
    cout << "Enter the number of minutes used during the evening:  ";
    cin >> PremNightMinutes;
    cout << endl;

    bAmount = PREM_BASIC_SERV_COST;
        if (PremDayMinutes > PREM_DAY_FREE_MIN)
{			
        bAmount = bAmount +(PremDayMinutes - PREM_DAY_FREE_MIN)
* PREM_DAY_OVER_CHRG;
	 
        if (PremNightMinutes > PREM_NIGHT_FREE_MIN)
           bAmount = bAmount + (PremNightMinutes - PREM_NIGHT_FREE_MIN) 
* PREM_NIGHT_OVER_CHRG;
           else		 
            bAmount = PREM_BASIC_SERV_COST;
}	

return bAmount;
}

In the main function case 'r' you are returning the value from the function in the variable AmountDue but you are printing the variable bAmount

This is not what you have done in the next case

Edited 6 Years Ago by abhimanipal: n/a

Thank you. Do I have my braces in the wrong place on the PremiumBill function? Its not reading the 'if' part of the stmt. Maybe I need to go back to square one, I feel so stupid this cant be that difficult. The function is supposed to set the return amount 'bAmount' to the stnd charge and then go to the 'if' stmt if that isn't true, drop to the next 'if' stmt, if that is true do the calculations and return the amount. When the 2nd 'if' is not true it should drop all the way thru to the 'else' and send back the basic cost.

I do not think that the logic that you have written for your premium bill is correct. Probably your premium bill function should be some thing like this

bAmount= basic_cost ;
if( day_mins_usage > day_mins _free)
{
// Add the extra charges here
}

if(night_mins_usage > night_mins_free)
{

// Add the extra charges
}

I hope you understood the mistake

I do not think that the logic that you have written for your premium bill is correct. Probably your premium bill function should be some thing like this

bAmount= basic_cost ;
if( day_mins_usage > day_mins _free)
{
// Add the extra charges here
}

if(night_mins_usage > night_mins_free)
{

// Add the extra charges
}

I hope you understood the mistake

That did take care of the issue. Thanks so much for your help. The braces seperate the 'if' stmts and it makes each a single stmt?

You are getting the compiler error because the variable bAmount is printed out in line 40 before it is assigned to any value. In that case (line 40) the correct variable should be AmountDue

This question has already been answered. Start a new discussion instead.