Trouble with calling functions
Oct 1, 2011 at 3:18pm Oct 1, 2011 at 3:18pm UTC
Hey everyone, I just had a quick question in regards to syntax. I have 6 functions. All of the functions work but I'm having trouble calling the functions. For example, I am having users first select what they will be paying with, cash or debit. Then, i am having them select a gas type. *NOW this is what im having trouble with* I'm calling the function that selects the gas, say Diesel at 3.99. I've been trying to debug this for about an hour but when I pick any value, say Diesel at 3.99 and insert the gallons, 10. It reads $680.00.
I feel like pulling my hair out because I can't find any errors in the syntax and feel like I have it right. Anyways, any help would be greatly appreciated. I'm not asking you to do it for me but just kindly point me in the right direction. Thank you so much for your help in advance. (The syntax is mostly right, i had to chop some out for it to fit).
How many gallons you would? Please enter a positive float value: 10
Your bill is: $680.00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
#include <iostream>
#include <iomanip>
using namespace std;
const char CASH = 'C' ;
const char CREDIT = 'D' ;
const char REGULAR = 'R' ;
const char EXTRA = 'X' ;
const char PREMIUM = 'P' ;
const char DIESEL = 'D' ;
const float REGULARPRICE = 3.42;
const float EXTRAPRICE = 3.85;
const float PREMIUMPRICE = 4.05;
const float DIESELPRICE = 3.99;
//---------------------------------------------------------------
// ConfirmChoice
// Parameters: The type of gas selected
// Purpose: Confirms the users gas purchase before filling up
// Returns: true if the user confirms the selection, false otherwise
//--------------------------------------------------------------
bool ConfirmChoice(const char GasType) //function #1
{
char Choice;
bool Confirmed;
// Print out their selection
cout << "\nYou have chosen to purchase " ;
cout << fixed << setprecision(2);
switch (GasType)
{
case REGULAR:
cout << "Regular gas at a price of $" ;
cout << REGULARPRICE << ".\n" ;
break ;
case EXTRA:
cout << "Extra gas at a price of $" ;
cout << EXTRAPRICE << ".\n" ;
break ;
case PREMIUM:
cout << "Premium gas at a price of $" ;
cout << PREMIUMPRICE << ".\n" ;
break ;
case DIESEL:
cout << "Deisel gas at a price of $" ;
cout << DIESELPRICE << ".\n" ;
break ;
}
//Confirm ?
cout << "Do you wish to confirm your purchase? Enter Y or N: " ;
cin >> Choice;
Choice = toupper(Choice);
while (Choice != 'Y' && Choice != 'N' )
{
cout << "Invalid selection. Please enter either Y or N: " ;
cin >> Choice;
Choice = toupper(Choice);
}
Confirmed = (Choice == 'Y' );
//Check Confirmation
if (Confirmed)
cout << "You have confirmed your choice.\n" << endl;
else
cout << "You have not confirmed your choice.\n" << endl;
return (Confirmed);
}
void CalculateChange(const float ChangeDue) // Function#2
{
int Change = 0;
int Dollars = 0;
int Quarters = 0;
int Dimes = 0;
int Nickels = 0;
int Pennies = 0;
//Compute Change
Change = ChangeDue * 100;
Dollars = Change / 100;
Change = Change % 100;
Quarters = Change / 25;
Change = Change % 25;
Dimes = Change / 10;
Change = Change % 10;
Nickels = Change / 5;
Pennies = Change % 5;
//Print out Change
cout << "Your change is \n\t" << Dollars << " Dollars\n\t"
<< Quarters << " Quarters\n\t" << Dimes << " Dimes\n\t"
<< Nickels << " Nickels\n\t" << Pennies << " Pennies\n" ;
}
float CalculateCost(const float PricePerGallon)
{
float Gallons;
float Cost;
cout << "How many gallons you would? Please enter a positive float value: " ;
cin >> Gallons;
while (Gallons < 0)
cout << "Invalid entry. Please re-enter: " ;
cin >> Gallons;
}
Cost = PricePerGallon * Gallons;
cout << "Your bill is: $" << fixed << setprecision(2) << Cost << endl;
return Cost;
}
//---------------------------------------------------------------------------
// Name: GetPaymentType
// Purpose: Ask the user how they want to pay, cash or credit
// Parameters: none
// Returns: char; value is Credit or Cash (global constants)
//---------------------------------------------------------------------------
char GetPaymentType() //Fuction #3
{
char Choice;
// Print the main menu describing the Gas Prices
cout << "+-------------------------------------------------------+\n" ;
cout << "+ Welcome to our Gas Station +\n" ;
cout << "+-------------------------------------------------------+\n" ;
cout << endl << endl;
// Cash or Credit Card
cout << "How would you like to pay?\n" ;
cout << "Enter C for cash or D for credit card: " ;
cin >> Choice;
Choice = toupper(Choice); // convert to uppercase
while (Choice != CASH && Choice != CREDIT)
{
cout << "Invalid choice. Please re-enter: " ;
cin >> Choice;
Choice = toupper(Choice);
}
return Choice;
void GetGasType(char &Choice) //Function#4
{
// Ask the customer what type of gas s/he prefers to buy
cout << "\nWhat type of gas would you like?\n" ;
cout << "\t" << REGULAR << " for Regular, Price = $" << REGULARPRICE << endl;
cout << "\t" << EXTRA << " for Extra, Price = $" << EXTRAPRICE << endl;
cout << "\t" << PREMIUM << " for Premium, Price = $" << PREMIUMPRICE << endl;
cout << "\t" << DIESEL << " for Diesel, Price = $" << DIESELPRICE << endl;
//Get Gas Choice
cout << "Enter choice: " ;
cin >> Choice;
Choice = toupper(Choice); // convert to uppercase
while (Choice != REGULAR && Choice != EXTRA && Choice != PREMIUM
&& Choice != DIESEL)
{
cout << "Invalid choice. Please re-enter: " ;
cin >> Choice;
Choice = toupper(Choice);
}
}
//---------------------------------------------------------------------------
// Name: PayWithCash
// Purpose: Handles payment by cash. Asks the user for the money until
// they enter enough, then updates the ChangeDue parameter
// Parameters: const float Cost - the amount due for the purchase
// float &ChangeDue - the amount of change due
// Returns: Nothing
//---------------------------------------------------------------------------
void PayWithCash(const float Cost, float &ChangeDue) //Fuction$5
{
float CashOffered;
// Pay in Cash
cout << "Please enter enough cash. Your bill is $" << Cost << ": $ " ;
cin >> CashOffered;
//Check Sufficiency
while (CashOffered < Cost)
{
cout << "That is not enough to pay for your purchase!\n"
<< " Please enter at least $" << Cost << ": " ;
cin >> CashOffered;
}
//Calculate Change
ChangeDue = CashOffered - Cost;
}
//---------------------------------------------------------------------------
// Name: PayWithCredit
// Purpose: Handles payment by credit. Basically, just prints a statement
// telling them that their card will be charged.
// Parameters: const float Cost - the amount due for the purchase
// Returns: nothing
//---------------------------------------------------------------------------
void PayWithCredit(const float Cost) //Fuction#6
{
cout << "Your credit card will be billed for $" << Cost << ".\n" ;
}
//---------------------------------------------------------------------------
// This is the main program that you need to write
//---------------------------------------------------------------------------
int main()
{
// Declarations
char GChoice; // gas type: Regular, Extra, etc..
char PChoice; // payment choice: cash or credit card
bool Confirmed; // did the user confirm the selection
float Cost; // the cost of the gas puchased
float ChangeDue; // the amount of change owed (for cash purchases)
cout << "-----------------" << endl; //
PChoice=GetPaymentType();
if (PChoice=='C' || PChoice=='D' )
{ GetGasType(PChoice); }
ConfirmChoice(PChoice);
CalculateCost(PChoice);
Last edited on Oct 1, 2011 at 11:05pm Oct 1, 2011 at 11:05pm UTC
Oct 1, 2011 at 4:04pm Oct 1, 2011 at 4:04pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
#include <iostream>
#include <iomanip>
using namespace std;
const char CASH = 'C' ;
const char CREDIT = 'D' ;
const char REGULAR = 'R' ;
const char EXTRA = 'X' ;
const char PREMIUM = 'P' ;
const char DIESEL = 'D' ;
const float REGULARPRICE = 3.42;
const float EXTRAPRICE = 3.85;
const float PREMIUMPRICE = 4.05;
const float DIESELPRICE = 3.99;
//---------------------------------------------------------------
// ConfirmChoice
// Parameters: The type of gas selected
// Purpose: Confirms the users gas purchase before filling up
// Returns: true if the user confirms the selection, false otherwise
//--------------------------------------------------------------
bool ConfirmChoice(const char GasType) //function #1
{
char Choice;
bool Confirmed;
// Print out their selection
cout << "\nYou have chosen to purchase " ;
cout << fixed << setprecision(2);
switch (GasType)
{
case REGULAR:
cout << "Regular gas at a price of $" ;
cout << REGULARPRICE << ".\n" ;
break ;
case EXTRA:
cout << "Extra gas at a price of $" ;
cout << EXTRAPRICE << ".\n" ;
break ;
case PREMIUM:
cout << "Premium gas at a price of $" ;
cout << PREMIUMPRICE << ".\n" ;
break ;
case DIESEL:
cout << "Deisel gas at a price of $" ;
cout << DIESELPRICE << ".\n" ;
break ;
}
//Confirm ?
cout << "Do you wish to confirm your purchase? Enter Y or N: " ;
cin >> Choice;
Choice = toupper(Choice);
while (Choice != 'Y' && Choice != 'N' )
{
cout << "Invalid selection. Please enter either Y or N: " ;
cin >> Choice;
Choice = toupper(Choice);
}
Confirmed = (Choice == 'Y' );
//Check Confirmation
if (Confirmed)
cout << "You have confirmed your choice.\n" << endl;
else
cout << "You have not confirmed your choice.\n" << endl;
return (Confirmed);
}
void CalculateChange(const float ChangeDue) // Function#2
{
int Change = 0;
int Dollars = 0;
int Quarters = 0;
int Dimes = 0;
int Nickels = 0;
int Pennies = 0;
//Compute Change
Change = ChangeDue * 100;
Dollars = Change / 100;
Change = Change % 100;
Quarters = Change / 25;
Change = Change % 25;
Dimes = Change / 10;
Change = Change % 10;
Nickels = Change / 5;
Pennies = Change % 5;
//Print out Change
cout << "Your change is \n\t" << Dollars << " Dollars\n\t"
<< Quarters << " Quarters\n\t" << Dimes << " Dimes\n\t"
<< Nickels << " Nickels\n\t" << Pennies << " Pennies\n" ;
}
float CalculateCost(const float PricePerGallon)
{
float Gallons;
float Cost;
cout << "How many gallons you would? Please enter a positive float value: " ;
cin >> Gallons;
while (Gallons < 0)
cout << "Invalid entry. Please re-enter: " ;
cin >> Gallons;
}
Cost = PricePerGallon * Gallons;
cout << "Your bill is: $" << fixed << setprecision(2) << Cost << endl;
return Cost;
}
//---------------------------------------------------------------------------
// Name: GetPaymentType
// Purpose: Ask the user how they want to pay, cash or credit
// Parameters: none
// Returns: char; value is Credit or Cash (global constants)
//---------------------------------------------------------------------------
char GetPaymentType() //Fuction #3
{
char Choice;
// Print the main menu describing the Gas Prices
cout << "+-------------------------------------------------------+\n" ;
cout << "+ Welcome to our Gas Station +\n" ;
cout << "+-------------------------------------------------------+\n" ;
cout << endl << endl;
// Cash or Credit Card
cout << "How would you like to pay?\n" ;
cout << "Enter C for cash or D for credit card: " ;
cin >> Choice;
Choice = toupper(Choice); // convert to uppercase
while (Choice != CASH && Choice != CREDIT)
{
cout << "Invalid choice. Please re-enter: " ;
cin >> Choice;
Choice = toupper(Choice);
}
return Choice;
void GetGasType(char &Choice) //Function#4
{
// Ask the customer what type of gas s/he prefers to buy
cout << "\nWhat type of gas would you like?\n" ;
cout << "\t" << REGULAR << " for Regular, Price = $" << REGULARPRICE << endl;
cout << "\t" << EXTRA << " for Extra, Price = $" << EXTRAPRICE << endl;
cout << "\t" << PREMIUM << " for Premium, Price = $" << PREMIUMPRICE << endl;
cout << "\t" << DIESEL << " for Diesel, Price = $" << DIESELPRICE << endl;
//Get Gas Choice
cout << "Enter choice: " ;
cin >> Choice;
Choice = toupper(Choice); // convert to uppercase
while (Choice != REGULAR && Choice != EXTRA && Choice != PREMIUM
&& Choice != DIESEL)
{
cout << "Invalid choice. Please re-enter: " ;
cin >> Choice;
Choice = toupper(Choice);
}
}
//---------------------------------------------------------------------------
// Name: PayWithCash
// Purpose: Handles payment by cash. Asks the user for the money until
// they enter enough, then updates the ChangeDue parameter
// Parameters: const float Cost - the amount due for the purchase
// float &ChangeDue - the amount of change due
// Returns: Nothing
//---------------------------------------------------------------------------
void PayWithCash(const float Cost, float &ChangeDue) //Fuction$5
{
float CashOffered;
// Pay in Cash
cout << "Please enter enough cash. Your bill is $" << Cost << ": $ " ;
cin >> CashOffered;
//Check Sufficiency
while (CashOffered < Cost)
{
cout << "That is not enough to pay for your purchase!\n"
<< " Please enter at least $" << Cost << ": " ;
cin >> CashOffered;
}
//Calculate Change
ChangeDue = CashOffered - Cost;
}
//---------------------------------------------------------------------------
// Name: PayWithCredit
// Purpose: Handles payment by credit. Basically, just prints a statement
// telling them that their card will be charged.
// Parameters: const float Cost - the amount due for the purchase
// Returns: nothing
//---------------------------------------------------------------------------
void PayWithCredit(const float Cost) //Fuction#6
{
cout << "Your credit card will be billed for $" << Cost << ".\n" ;
}
//---------------------------------------------------------------------------
// This is the main program that you need to write
//---------------------------------------------------------------------------
int main()
{
// Declarations
char GChoice; // gas type: Regular, Extra, etc..
char PChoice; // payment choice: cash or credit card
bool Confirmed; // did the user confirm the selection
float Cost; // the cost of the gas puchased
float ChangeDue; // the amount of change owed (for cash purchases)
cout << "-----------------" << endl; //
PChoice=GetPaymentType();
if (PChoice=='C' || PChoice=='D' )
{ GetGasType(PChoice); }
ConfirmChoice(PChoice);
CalculateCost(PChoice);
Your function header for
CalculateCost
has one parameter of type
const float
, yet in your call to that function you pass it a
char
which represents the chosen gas type. So it's using the ASCII keycode of that char as its 'price per gallon' value.
Last edited on Oct 1, 2011 at 4:06pm Oct 1, 2011 at 4:06pm UTC
Topic archived. No new replies allowed.