Stopping a function if wrong information is given

What am I missing to in this function to terminate the processing and print an error message if there is a wrong meter size input? Here is code:



//Header Files
#include <iostream> //needed for cin and cout chpt. 3
#include <conio.h> //needed for getch( )
#include <string> //needed for string



using namespace std;

double FindWaterCharge(double, double); //function prototype, similair to function heading chpt. 3
double FindSewerCharge(double , double , double); //function heading chpt. 3
double FindTotalCost(double, double, double, double); //function prototype, similair to function heading chpt. 3



//If I needed to declare constants I would declare them here, all upper case with _ b/w words
const double SANITATION_FEE = 20.41; //
const double STORM_WATER_FEE = 5.80; //
const double SEWER_PER_GALLON = .00372;


int main( )
{
//Declaration section
double meterSize; //size of meter at resident
double gallonsUsed; //total gallons used
double totalCost; //total cost of the water bill
double waterCharge; //total cost of the water charge on the bill
double sewerCharge; //total cost of the sewer charge on the bill
string accountNumber; //string for the account number

//Input section
cout << "Enter the account number" << endl;
cin >> accountNumber;
cout << "Enter the meter size" << endl;
cin >> meterSize;
cout << "Enter the gallons used" << endl;
cin >> gallonsUsed;
cout << endl << endl;


//Processing section
waterCharge = FindWaterCharge(meterSize, gallonsUsed); //function call
sewerCharge = FindSewerCharge(meterSize, gallonsUsed, SEWER_PER_GALLON); //function heading chpt. 3
totalCost = FindTotalCost(waterCharge, sewerCharge, SANITATION_FEE, STORM_WATER_FEE); //function call


//Output section
cout << "The account number is: " << accountNumber << endl;
cout << "The number of gallons used is: " << gallonsUsed << endl;
cout << "The total water charge is: $" << waterCharge << endl;
cout << "The total sewer charge is: $" << sewerCharge << endl;
cout << "The total amount of the bill is: $" << totalCost << endl;


getch( );
return 0;
}

double FindWaterCharge(double meterSize, double gallonsUsed) //function heading chpt. 3
//Preconditions: The value of meterSize and gallonsUsed is known
//Postconditions: The total cost of the water charge in the bill is returned
{
double baseWater;
double waterPerGallon;

if (meterSize == 0.625 || meterSize == .625)
baseWater = 3.61;
else if (meterSize == 0.75 || meterSize == .75)
baseWater = 4.23;
else if (meterSize == 1.0 || meterSize == 1)
baseWater = 6.14;

if (gallonsUsed <= 4000 && gallonsUsed > 0)
waterPerGallon = .00141;
else if (gallonsUsed >= 4001 && gallonsUsed <= 10000)
waterPerGallon = .00231;
else if (gallonsUsed >= 10001 && gallonsUsed <= 15000)
waterPerGallon = .00320;
else if (gallonsUsed > 15001)
waterPerGallon = .00370;

return (baseWater + (waterPerGallon * gallonsUsed));
}
double FindSewerCharge(double meterSize, double gallonsUsed, double SEWER_PER_GALLON) //function heading chpt. 3
//Preconditions: The value of meterSize, gallonsUsed and SEWER_PER_GALLON are known
//Postconditions: The total cost of the sewer charge in the bill is returned
{
double baseSewer;

if (meterSize == 0.625 || meterSize == .625)
baseSewer = 3.23;
else if (meterSize == 0.75 || meterSize == .75)
baseSewer = 3.78;
else if (meterSize == 1.0 || meterSize == 1)
baseSewer = 5.49;

return (baseSewer + (gallonsUsed * SEWER_PER_GALLON));
}
double FindTotalCost(double waterCharge, double sewerCharge, double SANITATION_FEE, double STORM_WATER_FEE) //function heading chpt. 3
//Preconditions: The value of waterCharge, sewerCharge, SANITATION_FEE and STORM_WATER_FEE are known
//Postconditions: The total cost of the bill is returned
{
return waterCharge + sewerCharge + SANITATION_FEE + STORM_WATER_FEE;
}





Thanks in advance.
Last edited on
oh...and nothing should be printed from a value-returning function. Thanks.
1
2
3
4
5
6
if (meterSize == 0.625 || meterSize == .625)
    baseWater = 3.61;
else if (meterSize == 0.75 || meterSize == .75)
    baseWater = 4.23;
else if (meterSize == 1.0 || meterSize == 1)
    baseWater = 6.14;


You need something like this:

1
2
else
    throw std::domain_error("Bad meter size");


http://www.cplusplus.com/reference/std/stdexcept/domain_error/

One thing I will caution you against is exact comparison of floating point values. This explains why: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.17


Here is what I modified in the code and it seamed to work. I don't know if this is the most efficient way to do it. Based on the parameters of the assignment in which that I cannot have anything printed in the value-returning function, it seams that your solution wouldn't work. Not that it wouldn't work, but saying for this assignment it wouldn't. Thank you for your help though and let me know what you think or if you found a better way.

//Header Files
#include <iostream> //needed for cin and cout chpt. 3
#include <conio.h> //needed for getch( )
#include <string> //needed for string



using namespace std;

double FindWaterCharge(double, double); //function prototype, similair to function heading chpt. 3
double FindSewerCharge(double , double , double); //function heading chpt. 3
double FindTotalCost(double, double, double, double); //function prototype, similair to function heading chpt. 3



//If I needed to declare constants I would declare them here, all upper case with _ b/w words
const double SANITATION_FEE = 20.41; //
const double STORM_WATER_FEE = 5.80; //
const double SEWER_PER_GALLON = .00372;


int main( )
{
//Declaration section
double meterSize; //size of meter at resident
double gallonsUsed; //total gallons used
double totalCost; //total cost of the water bill
double waterCharge; //total cost of the water charge on the bill
double sewerCharge; //total cost of the sewer charge on the bill
int counter; //counter needed to either end program or continue based on meter size input correction
string accountNumber; //string for the account number

//Input section
cout << "Enter the account number: " << endl;
cin >> accountNumber;
cout << "Enter the meter size: " << endl;
cin >> meterSize;
cout << "Enter the gallons used: " << endl;
cin >> gallonsUsed;
cout << endl << endl;


//Processing section
waterCharge = FindWaterCharge(meterSize, gallonsUsed); //function call
sewerCharge = FindSewerCharge(meterSize, gallonsUsed, SEWER_PER_GALLON); //function heading chpt. 3
totalCost = FindTotalCost(waterCharge, sewerCharge, SANITATION_FEE, STORM_WATER_FEE); //function call



//Output section
while ((meterSize == 0.625 || meterSize == .625 || meterSize == 0.75 || meterSize == .75 || meterSize == 1.0 || meterSize == 1) && counter < 1)
{
counter++;
cout << "The account number is: " << accountNumber << endl;
cout << "The number of gallons used is: " << gallonsUsed << endl;
cout << "The total water charge is: $" << waterCharge << endl;
cout << "The total sewer charge is: $" << sewerCharge << endl;
cout << "The total amount of the bill is: $" << totalCost << endl << endl;

}
if (counter == 1)
cout << endl;
else
cout << "Error in meter size";



getch( );
return 0;
}

double FindWaterCharge(double meterSize, double gallonsUsed) //function heading chpt. 3
//Preconditions: The value of meterSize and gallonsUsed is known
//Postconditions: The total cost of the water charge in the bill is returned
{
double baseWater;
double waterPerGallon;

if (meterSize == 0.625 || meterSize == .625)
baseWater = 3.61;
else if (meterSize == 0.75 || meterSize == .75)
baseWater = 4.23;
else if (meterSize == 1.0 || meterSize == 1)
baseWater = 6.14;

if (gallonsUsed <= 4000 && gallonsUsed > 0)
waterPerGallon = .00141;
else if (gallonsUsed >= 4001 && gallonsUsed <= 10000)
waterPerGallon = .00231;
else if (gallonsUsed >= 10001 && gallonsUsed <= 15000)
waterPerGallon = .00320;
else if (gallonsUsed > 15001)
waterPerGallon = .00370;

return (baseWater + (waterPerGallon * gallonsUsed));
}
double FindSewerCharge(double meterSize, double gallonsUsed, double SEWER_PER_GALLON) //function heading chpt. 3
//Preconditions: The value of meterSize, gallonsUsed and SEWER_PER_GALLON are known
//Postconditions: The total cost of the sewer charge in the bill is returned
{
double baseSewer;

if (meterSize == 0.625 || meterSize == .625)
baseSewer = 3.23;
else if (meterSize == 0.75 || meterSize == .75)
baseSewer = 3.78;
else if (meterSize == 1.0 || meterSize == 1)
baseSewer = 5.49;

return (baseSewer + (gallonsUsed * SEWER_PER_GALLON));
}
double FindTotalCost(double waterCharge, double sewerCharge, double SANITATION_FEE, double STORM_WATER_FEE) //function heading chpt. 3
//Preconditions: The value of waterCharge, sewerCharge, SANITATION_FEE and STORM_WATER_FEE are known
//Postconditions: The total cost of the bill is returned
{
return waterCharge + sewerCharge + SANITATION_FEE + STORM_WATER_FEE;
}



Thank you again.
That isn't printing anything; it's throwing an exception.

http://www.cplusplus.com/doc/tutorial/exceptions/
Throwing exceptions doesn't print anything. You can just catch the exception and return or whatever.
Thanks for the advice.
Topic archived. No new replies allowed.