Post: C++ Help
12-07-2011, 02:49 AM #1
fightinillini94
Climbing up the ladder
(adsbygoogle = window.adsbygoogle || []).push({}); Hey guys,

So in my computer programming class we are tasked with making a concession stand program that outputs the change in dollars, quarters, dimes, nickels, and pennies. Everything works except the change output. In some instances the pennies are off by one. One instance is when the sub total is $4. I feel like the problem lies within the calculation of the tax but I cannot figure out what the problem is. If anybody can help it would be much appreciated

Thanks

Here is my code:

    //Justin Vartanian
//11-28-11
//Operate a concession stand

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main(){

char name[10];
int run_again = 1;
int concession_answer = 0;


cout << "What is your first name?" << endl;
cin >> name;
system("cls");

do{

int hamburger_number = 0, cheeseburger_number = 0, hot_dog_number = 0, brat_number = 0, soda_number = 0, bottled_water_number = 0, gatorade_number = 0;
float sub_total = 0;
float tax = 0;
float final_total = 0;
float money_given = 0;
float change = 0;
int remainder = 0;
int change_decimal = 0;
int dollars = 0;
int quarters = 0;
int dimes = 0;
int nickels = 0;
int pennies = 0;


do{
cout << "Hello " << name << ", what would you like to order today?" << endl;
cout << "1- Hamburger for $1.25" << endl;
cout << "2- Cheeseburger for $1.50" << endl;
cout << "3- Hot dog for $1.00" << endl;
cout << "4- Brat for $1.25" << endl;
cout << "5- Soda for $1.00" << endl;
cout << "6- Bottled Water for $2.00" << endl;
cout << "7- Gatorade for $1.75" << endl;
cout << "8- I am finished ordering" << endl;
cin >> concession_answer;

switch (concession_answer){
case 1:
hamburger_number++;
sub_total = sub_total + 1.25;
break;
case 2:
cheeseburger_number++;
sub_total = sub_total + 1.5;
break;
case 3:
hot_dog_number++;
sub_total = sub_total + 1;
break;
case 4:
brat_number++;
sub_total = sub_total + 1.25;
break;
case 5:
soda_number++;
sub_total = sub_total + 1;
break;
case 6:
bottled_water_number++;
sub_total = sub_total + 2;
break;
case 7:
gatorade_number++;
sub_total = sub_total + 1.75;
break;
}
}while(concession_answer != Cool Man (aka Tustin);

system("cls");

cout << name << "'s order:" << endl;
cout << endl;
cout << endl;
cout << hamburger_number << " Hamburger(s)" << endl;
cout << cheeseburger_number << " Cheeseburger(s)" << endl;
cout << hot_dog_number << " Hot dog(s)" << endl;
cout << brat_number << " Brat(s)" << endl;
cout << soda_number << " Soda(s)" << endl;
cout << bottled_water_number << " Bottled Water(s)" << endl;
cout << gatorade_number << " Gatorade(s)" << endl;
cout << endl;
cout << fixed << setprecision(2) << "Subtotal: $" << sub_total << endl;
cout << endl;
tax = sub_total * .065;
cout << "Tax: $" << tax << endl;
cout << endl;
final_total = tax + sub_total;
cout << "Final Total: $" << final_total << endl;
system("pause");
system("cls");

cout << "How much money are you giving me?" << endl;
cin >> money_given;

change = money_given - final_total;
dollars = change;
change_decimal = (change - dollars) * 100;

quarters = change_decimal / 25;
remainder = change_decimal % 25;

dimes = remainder / 10;
remainder = remainder % 10;

nickels = remainder / 5;
remainder = remainder % 5;

pennies = remainder / 1;
remainder = remainder % 1;

cout << "Your change is:" << endl;
cout << dollars << " Dollars" << endl;
cout << quarters << " Quarters" << endl;
cout << dimes << " Dimes" << endl;
cout << nickels << " Nickels" << endl;
cout << pennies << " Pennies" << endl;

system("pause");
system("cls");
cout << "Would you like to run the program again?" << endl;
cout << "1- Yes" << endl;
cout << "2- No" << endl;
cin >> run_again;
if(run_again == 1){
system("cls");
}
}while(run_again == 1);





system("pause");
return 0;
}
12-11-2011, 10:17 PM #2
The problem is you need to be rounding everything out to the 100th place since you are dealing with money. To fix your problem, go to the code "final_total = tax + sub_total;" and change it to "final_total = floor(((tax + sub_total) * 100 )) / 100;" This will get the total, multiply it by 100, get the nearest whole number with the floor command and then divide it by 100 to get the exact 100th place.

The following user thanked MoBaTeY for this useful post:

fightinillini94

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo