Hi there! I have written a program with fixed costs for a vacation. I need to add sales tax in for the total amount but this needs to be floating. Also, I had intended this to be a user input based program, but without knowing the float code for calculation, I dont know how to proceed.Any pointers or help would be awesome! Thanks!

``````#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

//declare function prototypes
void getInput(double& airtravelCost, int& hotelCost, int& foodCost, int& entCost, double& tripCost);
double airtravelCalculator();
int hotelCalculator();
int foodCalculator();
int entCalculator();
double printResults(double airtravelCost, int hotelCost, int foodCost, int entCost, double tripCost);

//named constants
double planeGasPerMile = .65;
int downtownHotel = 85;
int cheapHotel= 45;
int hostelHotel = 25;
int fastFoodMeal = 4;
int mediumFoodMeal = 8;
int fancyFoodMeal = 20;

int _tmain(int argc, _TCHAR* argv[])
{
//declare all variables in program
double airtravelCost;
//int airtravelMiles;
int hotelCost;
//int nightsIndowntown;
//int nightsIncheap;
//int nightsInhostel;
int foodCost;
//int mealsInFastFood;
//int mealsInMediumFood;
//int mealsInFancyFood;
int entCost;
double tripCost;
char newSpringbreak;

//Welcome message
cout << "Welcome to the spring break cost calculator." << endl;

cout << "Would you like to calculate the cost of a spring break?" << endl;
cout << "Please select 'Y' for yes or 'N' for no:" << endl;

cin >> newSpringbreak;

switch(newSpringbreak)
{
case 'Y': case 'y':
break;
case 'N': case 'n':
break;
default:
cout << "You have chosen an invalid selection." << endl;
cout << "Please select 'Y' for yes or 'N' for no:" << endl;

cin >> newSpringbreak;
break;
}
//calling functions
while (newSpringbreak == 'Y'|| newSpringbreak == 'y')
{
getInput(airtravelCost, hotelCost, foodCost, entCost, tripCost);

printResults(airtravelCost, hotelCost, foodCost, entCost, tripCost);

cout << "Would you like to calculate the cost of a spring break?" << endl;
cout << "Please select 'Y' for yes or 'N' for no:" << endl;

cin >> newSpringbreak;

switch(newSpringbreak)
{
case 'Y': case 'y':
break;
case 'N': case 'n':
break;
default:
cout << "You have chosen an invalid selection." << endl;
cout << "Please select 'Y' for yes or 'N' for no:" << endl;

cin >> newSpringbreak;
break;
}
}
return 0;
}

void getInput(double& airtravelCost, int& hotelCost, int& foodCost, int& entCost, double& tripCost)
{
airtravelCost = airtravelCalculator();
hotelCost = hotelCalculator();
foodCost = foodCalculator();
entCost = entCalculator();
}

double airtravelCalculator()
{
//declare variables
double airtravelCost;
int airtravelMiles;

cout << "Enter the total miles traveled by plane: ";
cin >> airtravelMiles;
cout << endl;

flightCost = (airtravelMiles * planeGasPerMile);

return airtravelCost;

}

int hotelCalculator()
{
//declare variables
int hotelCost;
int nightsIndowntown;
int nightsIncheap;
int nightsInhostelr;

cout << "How many nights will be spent in downtown hotels: ";
cin >> nightsIndowntown;
cout << endl;

cout << "How many nights will be  spent in cheap hotels: ";
cin >> nightsIncheap;
cout << endl;

cout << "How many nights will be spent in hostel hotels: ";
cin >> nightsInhostel;
cout << endl;

hotelCost = (nightsIndowntown * downtownHotel + nightsIncheap * cheapHotel + nightsInhostel * hostelHotel);

return hotelCost;
}

int foodCalculator()
{
//declare variables
int foodCost;
int mealsInFastFood;
int mealsInMediumFood;
int mealsInFancyFood;

cout << "How many meals will be eaten in fast food restaurants: ";
cin >> mealsInFastFood;
cout << endl;

cout << "How many meals will be eaten in medium priced restaurants: ";
cin >> mealsInMediumFood;
cout << endl;

cout << "How many meals will be eaten in high priced restaurants: ";
cin >> mealsInFancyFood;
cout << endl;

foodCost = (mealsInFastFood * fastFoodMeal + mealsInMediumFood * mediumFoodMeal + mealsInFancyFood * fancyFoodMeal);

return foodCost;
}

int entCalculator()
{
//declare variables
int entCost;

cout << "How much will you spend on entertainment: ";
cin >> entCost;
cout << endl;

return entCost;
}

double printResults(double airtravelCost, int hotelCost, int foodCost, int entCost, double tripCost)
{

//Calculates cost of entire trip
tripCost = (airtravelCost + hotelCost + foodCost + entCost);

//Displaying output
cout << "The cost of the airtravel is: " << flightCost << endl;
cout << "The total cost of the hotels is: " << hotelCost << endl;
cout << "The total cost of food is: " << foodCost << endl;
cout << "The total cost of entertainment is: " << entCost << endl;
cout << "The entire cost of the trip is: " << tripCost << endl;

return tripCost;
}``````

Your question is not very clear. What do you mean by "this needs to be floating" and "without knowing the float code for calculation"? That the sales tax rate can vary? Or that you need to use a floating point value (which can be of type float or type double)?

Really, the first thing you should do is clear up errors in the code posted - you mix up some variable names, causing this to not even compile.

The main organization - a switch for initial error check, then a loop that again includes this switch, is pretty awkward and won't function as I think you want it to. The switch should do more than just give an error message, let it do it's job. Here's a model to follow.

``````do
{

cin >> option;

switch( option )
{
case 'Y': //do the calculation
getInput(airtravelCost, hotelCost, foodCost, entCost, tripCost);

printResults(airtravelCost, hotelCost, foodCost, entCost, tripCost);
break;

case 'N':  //quiting
cout << "Goodbye" <<endl;  //let user know program is ending
break;

cout << "You have chosen an invalid selection." << endl;
cout << "Please select 'Y' for yes or 'N' for no:" << endl;
}
}while( option != 'N' );``````

Thanks for the reply! I guess I was not very clear with my problem. First, this program needs to be user input only, no fixed values. I set it up with constants because I was unclear of the way to be a variable input program. Second, I was also unclear about how to add in a sales tax to every applicable item. I will try your modification and try to compile and report back. Thanks!

Well, it sounds like you need to ask the user for values for all those costs, one time at the beginning. Store them to variables, and add the appropriate variables as parameters to the functions that need them.

If sales tax is applicable to all expenses, and it's the same for all categories (not necessarily so in real life) then you just need to figure it based on the total expense you come up with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.