Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com C++ help

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 09-30-2012, 03:00 AM PM User | #1 pbracing33b New Coder   Join Date: Mar 2012 Posts: 70 Thanks: 6 Thanked 0 Times in 0 Posts 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 #include #include 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<<"Please enter your destination: "<"; 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 { cout<<"Please enter a valid Character"<
 09-30-2012, 04:19 PM PM User | #2 oracleguy Rockstar Coder   Join Date: Jun 2002 Location: USA Posts: 9,054 Thanks: 1 Thanked 322 Times in 318 Posts 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:Get user input and set destination variables based on input Based on destination determine initial costs Calculate if any discounts apply Apply discounts Display results You should apply the 6 day discount and then you would apply the early reservation discount. __________________ OracleGuy
 10-01-2012, 09:08 PM PM User | #3 pbracing33b New Coder   Join Date: Mar 2012 Posts: 70 Thanks: 6 Thanked 0 Times in 0 Posts 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 #include #include 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<<"Please enter your destination: "<"; 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 { cout<<"Please enter a valid Character"< 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<
 10-02-2012, 06:22 PM PM User | #4 oracleguy Rockstar Coder   Join Date: Jun 2002 Location: USA Posts: 9,054 Thanks: 1 Thanked 322 Times in 318 Posts 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. __________________ OracleGuy
 10-02-2012, 06:41 PM PM User | #5 pbracing33b New Coder   Join Date: Mar 2012 Posts: 70 Thanks: 6 Thanked 0 Times in 0 Posts 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!
 10-03-2012, 01:57 AM PM User | #6 oracleguy Rockstar Coder   Join Date: Jun 2002 Location: USA Posts: 9,054 Thanks: 1 Thanked 322 Times in 318 Posts Can you post your latest code? __________________ OracleGuy
 10-03-2012, 03:25 AM PM User | #7 pbracing33b New Coder   Join Date: Mar 2012 Posts: 70 Thanks: 6 Thanked 0 Times in 0 Posts 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 #include #include 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<<"Please enter your destination: "<"; 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 { cout<<"Please enter a valid Character"< 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<
 10-03-2012, 07:07 PM PM User | #8 oracleguy Rockstar Coder   Join Date: Jun 2002 Location: USA Posts: 9,054 Thanks: 1 Thanked 322 Times in 318 Posts 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. __________________ OracleGuy
10-03-2012, 08:35 PM   PM User | #9
pbracing33b
New Coder

Join Date: Mar 2012
Posts: 70
Thanks: 6
Thanked 0 Times in 0 Posts
Quote:
 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-03-2012, 10:20 PM PM User | #10 oracleguy Rockstar Coder   Join Date: Jun 2002 Location: USA Posts: 9,054 Thanks: 1 Thanked 322 Times in 318 Posts 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. __________________ OracleGuy

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 04:09 AM.