Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## C++ help

I have and issue with my code, I have the code almost dead on except for one thing and it is a math issue, and I am not that good at math to figure out how to calculate this formula.

Here is what we need to do for this program:

The Holiday Travel Agency is putting together some travel packages for spring break, and they would like a program that will help them calculate the cost of a customer’s trip. They have 4 destinations which they will offer at different prices:

Destination Rate per day(hotel) Air fare
Florida \$ 70.00 \$ 175.55
Texas \$ 60.00 \$ 225.98
Cancun \$100.00 \$ 474.89
Bahamas \$ 95.00 \$ 355.98

In order to encourage people to sign up early and stay longer, they have the following discounts scheduled.

1. If they will be staying for more than 6 days there will be a price reduction based on their destination. (Hint: a nested if might work nicely here)

- Florida and Cancun: one day free
- Texas and Bahamas: 50% off the airfare

2. There is also a discount based on how far ahead the reservation is made. This discount is to be applied to the bill after long stay discounts has been deducted.
(you can use a subtotal)

- Reservation 1 - 4 weeks in advance no discount
- Reservation 5 - 6 weeks in advance 5% discount
- Reservation 7-9 weeks in advance 8% discount
- Reservation 10 or more weeks in advance 10% discount

Input: For each customer, the user will need to enter the number of weeks before the trip, the number of days they are staying and the destination (“F for Florida, “T” for Texas, “C” for Cancun, or “B” for Bahamas). Use a menu for the destinations. The input must be in the specific order listed here! Use good prompts to ask the user for the data. After you input the destination, use an if/else/if statement to change the destination letter to the destination word. ( change “F” which the user will enter to “Florida” which will be used in your output.)

Output: The input and output of the Project should look similar to the following. Use a desk check to validate your result. It is possible that the discount could be zero. The final bill should be the cost of hotel plus the cost of airfare minus the two discounts.

And here is my code:
Code:
```// Program #3 – Vacation Program
// Programmer: 236_Blair_Phil_P3
// Class:  CS 2010, Fall 2012
// Class Time: 1030-1120
// Due Date: Oct. 10, 2012
// Purpose of program: This program is going to help a user figure out when to schedule or how to save money and schedule early for their vacation. How much they saved is based upon when they book their vacation.

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

using namespace std;

int main()
{
//Declaration Section
double rate;
double discountRate;
int days;
int weeks;
char destination;
double discount;
double earlyDiscount;
double airFare;
double airFareDiscount;
double total;
string location;
double cost;
double discount1;

//Constants
const double FLORIDA_RATE = 70.00;
const double FLORIDA_AIR_FARE = 175.55;
const double TEXAS_RATE = 60.00;
const double TEXAS_AIR_FARE = 225.98;
const double CANCUN_RATE = 100.00;
const double CANCUN_AIR_FARE = 474.89;
const double BAHAMAS_RATE = 95.00;
const double BAHAMAS_AIR_FARE = 355.98;

//Input Section
cout<<"Enter the number of weeks before the trip: ";
cin>>weeks;
cout<<"How many days are you staying: ";
cin>>days;
cout<<"F for Florida"<<endl;
cout<<"T for Texas"<<endl;
cout<<"C for Cancun"<<endl;
cout<<"B for Bahamas"<<endl;
cout<<"=======>";
cin>>destination;

//Processing Section

if(destination == 'F'|| destination == 'f')
{
location = "Florida";
rate = FLORIDA_RATE * days;//possibly move these variables up to the declaration section
airFare = FLORIDA_AIR_FARE;
cost = rate + airFare;
discount =  FLORIDA_RATE * 1;
discountRate = rate + airFare - discount;
if (days > 6)
{
discount;
discountRate;
}
else
{
discount = 0;
}

if(weeks <= 4)//early discount
{
earlyDiscount = 0;
}
else if(weeks <= 6)
{
earlyDiscount = .05;
}
else if(weeks <= 9)
{
earlyDiscount = .08;
}
else if(weeks >= 10)
{
earlyDiscount = .10;
}
else
{
discount = 0;
}
}

else if(destination == 'T'|| destination == 't')
{
location = "Texas";
rate = TEXAS_RATE* days;
airFare = TEXAS_AIR_FARE;
if (days > 6)
{
airFareDiscount = airFare * .50;
discount = airFareDiscount;
discountRate = rate + airFare - discount;
}
else
{
discount = 0;
}
if(weeks <= 4)//early discount
{
earlyDiscount = 0;
}
else if(weeks <= 6)
{
earlyDiscount = discountRate * .05;
}
else if(weeks <= 9)
{
earlyDiscount = discountRate  * .08;
}
else if(weeks >= 10)
{
earlyDiscount = discountRate * .10;
}
}
else if(destination == 'C'|| destination == 'c')
{
location = "Cancun";
rate = CANCUN_RATE * days;
airFare = CANCUN_AIR_FARE;
if (days > 6)//long stay discount
{
discount =  CANCUN_RATE * 1;
discountRate = rate + airFare - discount;
}
else
{
discount = 0;
}
if(weeks <= 4)//early discount
{
earlyDiscount = 0;
}
else if(weeks <= 6)
{
earlyDiscount = discountRate * .05;
}
else if(weeks <= 9)
{
earlyDiscount = discountRate  * .08;
}
else if(weeks >= 10)
{
earlyDiscount = discountRate * .10;
}
}
else if(destination == 'B'|| destination == 'b')
{
location = "Bahamas";
rate = BAHAMAS_RATE * days;
airFare = BAHAMAS_AIR_FARE;
if (days > 6)//long discount
{
airFareDiscount = airFare * .50;
discount = airFareDiscount;
discountRate = rate + airFare - discount;
}
else
{
discount = 0;
}
if(weeks <= 4)//early discount
{
earlyDiscount = 0;
}
else if(weeks <= 6)
{
earlyDiscount =  discountRate * .05;
}
else if(weeks <= 9)
{
earlyDiscount = discountRate  * .08;
}
else if(weeks >= 10)
{
earlyDiscount = discountRate * .10;
}
discount1 = discountRate * earlyDiscount;
total = rate + airFare - discount - discount1;
}
else
{
system("pause");
return 0;
}

//Output Section
cout << fixed << showpoint << setprecision(2);
cout<<endl;
cout<<"=================================================================="<<endl;
cout<<"=================================================================="<<endl;
cout<<"           Holdiday Travel Agency"<<endl;
cout<<"              Customer Bill"<<endl;
cout<<endl;
cout<<"Destination:               "<<location<<endl;
cout<<"Number of weeks to go:     "<<weeks<<endl;
cout<<"Number of days to stay:    "<<days<<endl;
cout<<endl;
cout<<"Cost of Hotel:             \$  "<<setw(10)<<rate<<endl;
cout<<"Cost of Airfare:           \$  "<<setw(10)<<airFare<<endl;
cout<<"Longer stay discount:      \$  "<<setw(10)<<discount<<endl;
cout<<"Early signup discount:     \$  "<<setw(10)<<discount1<<endl;
cout<<endl;
cout<<"Final Bill:                \$  "<<setw(10)<<total<<endl;
cout<<"=================================================================="<<endl;
cout<<"=================================================================="<<endl;
system("pause");
return 0;
}```
The only part that I don't have working properly is if the user books his stay 4 or more weeks in advance and only stays less then 6 days. If this happens my calculations are off. So I'm not sure how to correct this issue, I know it is the way my math should be laid out, but I am not sure how to lay out this formula for this program,

any help that is given would be greatly appreciated.

Thanks.

oh and btw this is in visual studio C++

2. Well you have the same code duplicated several times in your function, this is bad. You can re-structure the code so that isn't the case and it will also reduce the complexity of the code. That might make it easier to debug not to mention the amount of code will go down significantly.

I would structure it like this:
1. Get user input and set destination variables based on input
2. Based on destination determine initial costs
3. Calculate if any discounts apply
4. Apply discounts
5. Display results

You should apply the 6 day discount and then you would apply the early reservation discount.

3. Here is my updated code and it still does the same thing! Except it doesn't figure on their being no discounts, if no discounts apply then it gives me an arbitrary number.

Code:
```// Program #3 – Vacation Program
// Programmer: 236_Blair_Phil_P3
// Class:  CS 2010, Fall 2012
// Class Time: 1030-1120
// Due Date: Oct. 10, 2012
// Purpose of program: This program is going to help a user figure out when to schedule or how to save money and schedule early for their vacation. How much they saved is based upon when they book their vacation.

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

using namespace std;

int main()
{
//Declaration Section
double rate;
int days;
int weeks;
char destination;
double discount;
double earlyDiscount;
double airFare;
double total;
string location;
double subtotal;

//Constants
const double FLORIDA_RATE = 70.00;
const double FLORIDA_AIR_FARE = 175.55;
const double TEXAS_RATE = 60.00;
const double TEXAS_AIR_FARE = 225.98;
const double CANCUN_RATE = 100.00;
const double CANCUN_AIR_FARE = 474.89;
const double BAHAMAS_RATE = 95.00;
const double BAHAMAS_AIR_FARE = 355.98;
const double DISCOUNT_5_PERCENT = .05;
const double DISCOUNT_8_PERCENT = .08;
const double DISCOUNT_10_PERCENT = .10;

//Input Section
cout<<"Enter the number of weeks before the trip: ";
cin>>weeks;
cout<<"How many days are you staying: ";
cin>>days;
cout<<"F for Florida"<<endl;
cout<<"T for Texas"<<endl;
cout<<"C for Cancun"<<endl;
cout<<"B for Bahamas"<<endl;
cout<<"=======>";
cin>>destination;

//Processing Section

if(destination == 'F'|| destination == 'f')
{
location = "Florida";
rate = FLORIDA_RATE;
airFare = FLORIDA_AIR_FARE;

}
else if(destination == 'T'|| destination == 't')
{
location = "Texas";
rate = TEXAS_RATE;
airFare = TEXAS_AIR_FARE;

}
else if(destination == 'C'|| destination == 'c')
{
location = "Cancun";
rate = CANCUN_RATE;
airFare = CANCUN_AIR_FARE;

}
else if(destination == 'B'|| destination == 'b')
{
location = "Bahamas";
rate = BAHAMAS_RATE;
airFare = BAHAMAS_AIR_FARE;

}
else
{
system("pause");
return 0;
}
if (days > 6)
{
if (location == "Florida" || location == "Cancun")
{
discount = rate * 1;
}
else
{
discount = airFare * .50;
}
rate = rate * days;
subtotal = rate + airFare - discount;
}
if(weeks <= 4)//early discount
{
earlyDiscount = 0;
}
else if(weeks <= 6)
{
earlyDiscount =  subtotal * DISCOUNT_5_PERCENT;

}
else if(weeks <= 9)
{
earlyDiscount =  subtotal * DISCOUNT_8_PERCENT;
}
else if(weeks >= 10)
{
earlyDiscount =  subtotal * DISCOUNT_10_PERCENT;
}
total = subtotal  - earlyDiscount;

//Output Section
cout << fixed << showpoint << setprecision(2);
cout<<endl;
cout<<"=================================================================="<<endl;
cout<<"=================================================================="<<endl;
cout<<"           Holdiday Travel Agency"<<endl;
cout<<"              Customer Bill"<<endl;
cout<<endl;
cout<<"Destination:               "<<location<<endl;
cout<<"Number of weeks to go:     "<<weeks<<endl;
cout<<"Number of days to stay:    "<<days<<endl;
cout<<endl;
cout<<"Cost of Hotel:             \$  "<<setw(10)<<rate<<endl;
cout<<"Cost of Airfare:           \$  "<<setw(10)<<airFare<<endl;
cout<<"Longer stay discount:      \$  "<<setw(10)<<discount<<endl;
cout<<"Early signup discount:     \$  "<<setw(10)<<earlyDiscount<<endl;
cout<<endl;
cout<<"Final Bill:                \$  "<<setw(10)<<total<<endl;
cout<<"=================================================================="<<endl;
cout<<"=================================================================="<<endl;
system("pause");
return 0;
}```

4. That is because you aren't initializing your variables to anything. So if there are no discounts the discount variables are left at random values. Initialize them to zero when you declare them and it should work the way you want.

5. I totally forgot to initialized those variables and that solved that issue. But I'm not sure why the total isn't adding up, when ran if I just say neither has any discounts, or one or the other doesn't have discounts total is initialized to 0 but it still doesn't add the airFare nor the rate. So I'm not sure why this is!

6. Can you post your latest code?

7. Here is my code, I have noticed that if I move total = airFare + rate - discount - earlyDiscount; My code seems to work, except for when the user has a 7 week early sign up and 1 day then my code doesn't calculate the early discount. But everything else seems to work fine. So I am not sure as to why or what is going on here. Thanks for the help given so far and the help that will be given.

Code:
```// Program #3 – Vacation Program
// Programmer: 236_Blair_Phil_P3
// Class:  CS 2010, Fall 2012
// Class Time: 1030-1120
// Due Date: Oct. 10, 2012
// Purpose of program: This program is going to help a user figure out when to schedule or how to save money and schedule early for their vacation. How much they saved is based upon when they book their vacation.

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

using namespace std;

int main()
{
//Declaration Section
double rate =0;
int days;
int weeks;
char destination;
double discount = 0;
double earlyDiscount = 0;
double airFare = 0;
double total = 0;
string location;
double subtotal = 0;

//Constants
const double FLORIDA_RATE = 70.00;
const double FLORIDA_AIR_FARE = 175.55;
const double TEXAS_RATE = 60.00;
const double TEXAS_AIR_FARE = 225.98;
const double CANCUN_RATE = 100.00;
const double CANCUN_AIR_FARE = 474.89;
const double BAHAMAS_RATE = 95.00;
const double BAHAMAS_AIR_FARE = 355.98;
const double DISCOUNT_5_PERCENT = .05;
const double DISCOUNT_8_PERCENT = .08;
const double DISCOUNT_10_PERCENT = .10;

//Input Section
cout<<"Enter the number of weeks before the trip: ";
cin>>weeks;
cout<<"How many days are you staying: ";
cin>>days;
cout<<"F for Florida"<<endl;
cout<<"T for Texas"<<endl;
cout<<"C for Cancun"<<endl;
cout<<"B for Bahamas"<<endl;
cout<<"=======>";
cin>>destination;

//Processing Section

if(destination == 'F'|| destination == 'f')
{
location = "Florida";
rate = FLORIDA_RATE;
airFare = FLORIDA_AIR_FARE;

}
else if(destination == 'T'|| destination == 't')
{
location = "Texas";
rate = TEXAS_RATE;
airFare = TEXAS_AIR_FARE;

}
else if(destination == 'C'|| destination == 'c')
{
location = "Cancun";
rate = CANCUN_RATE;
airFare = CANCUN_AIR_FARE;

}
else if(destination == 'B'|| destination == 'b')
{
location = "Bahamas";
rate = BAHAMAS_RATE;
airFare = BAHAMAS_AIR_FARE;

}
else
{
system("pause");
return 0;
}
if (days > 6)
{
if (location == "Florida" || location == "Cancun")
{
discount = rate * 1;
}
else
{
discount = airFare * .50;
}
rate = rate * days;
subtotal = rate + airFare - discount;

}
if(weeks <= 4)//early discount
{
earlyDiscount = 0;
}
else if(weeks <= 6)
{
earlyDiscount =  subtotal * DISCOUNT_5_PERCENT;

}
else if(weeks <= 9)
{
earlyDiscount =  subtotal * DISCOUNT_8_PERCENT;

}
else if(weeks >= 10)
{
earlyDiscount =  subtotal * DISCOUNT_10_PERCENT;
}

subtotal = subtotal - earlyDiscount;
total = rate + airFare - discount - earlyDiscount;

//Output Section
cout << fixed << showpoint << setprecision(2);
cout<<endl;
cout<<"=================================================================="<<endl;
cout<<"=================================================================="<<endl;
cout<<"           Holdiday Travel Agency"<<endl;
cout<<"              Customer Bill"<<endl;
cout<<endl;
cout<<"Destination:               "<<location<<endl;
cout<<"Number of weeks to go:     "<<weeks<<endl;
cout<<"Number of days to stay:    "<<days<<endl;
cout<<endl;
cout<<"Cost of Hotel:             \$  "<<setw(10)<<rate<<endl;
cout<<"Cost of Airfare:           \$  "<<setw(10)<<airFare<<endl;
cout<<"Longer stay discount:      \$  "<<setw(10)<<discount<<endl;
cout<<"Early signup discount:     \$  "<<setw(10)<<earlyDiscount<<endl;
cout<<endl;
cout<<"Final Bill:                \$  "<<setw(10)<<total<<endl;
cout<<"=================================================================="<<endl;
cout<<"=================================================================="<<endl;
system("pause");
return 0;
}```

8. Ok, so you are only setting the rate and subtotal if the days are greater than 6. You need to do it even if the days are less. Add an else to calculate the subtotal if there is no discount for the number of days.

You can figure this out by stepping line by line through the program and inspecting the variables, inside the number of weeks if block you can see that the subtotal variable is 0 which means it was never assigned to.

9. Originally Posted by oracleguy
Ok, so you are only setting the rate and subtotal if the days are greater than 6. You need to do it even if the days are less. Add an else to calculate the subtotal if there is no discount for the number of days.

You can figure this out by stepping line by line through the program and inspecting the variables, inside the number of weeks if block you can see that the subtotal variable is 0 which means it was never assigned to.
Hey thanks alot.

I knew it had to be something like(days < 6 because it wasn't calculating it right) that and I swear I tried something like that before and it still wasn't working, but now for some reason I tried it again and now everything works fine.

Btw for future use how do I step through the program. For example if I set a break point at say line 200 how do I step through each line to check my code?

I'm a little rusty and I don't do this everday, so I need a refresher.

Thanks,

Phil

10. Depends on your debugger. Are you using an IDE? (As in Visual Studio, Eclipse, Code Blocks, etc.) There should be buttons/options for step into and step over, those execute the code line by line.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•