Jump to content

C++ Help

- - - - -

  • Please log in to reply
3 replies to this topic

#1
fightinillini94

fightinillini94

    Newbie

  • Members
  • Pip
  • 2 posts
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 != 8);

	

		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;

}


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
It is difficult to represent fractions with base 2, 10 x 0.1 cannot equal 10 with this logic:
1/10 = 0.000110011b = .099609375d
.099609375d x 10d = 0.960937d != 1d
It can be the largest possible representation of 0.1 without being 0.1*

You are also limited in the amount of bits of which can be stored for the fraction itself, within the floating point structure**.
Posted Image

quote said:

pennies are off by one.
You may wish to work only in integers, or pennies as long as your cost is not abnormally large (xxxxxxxx.xx).
int a = 190;
int b = 210;
float answer = (a+b) / 100.[B]0; [/B]//or 100.0f for floating point division. Integer division (6 / 4) may equal 0

Alexander.

*A possible option for correcting fraction rounding errors: Machine epsilon - Wikipedia, the free encyclopedia
**A reference to the data type I was discussing: Single-precision floating-point format - Wikipedia, the free encyclopedia

Attached Files


Edited by Alexander, 07 December 2011 - 07:48 PM.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
fightinillini94

fightinillini94

    Newbie

  • Members
  • Pip
  • 2 posts

Alexander said:

It is difficult to represent fractions with base 2, 10 x 0.1 cannot equal 10 with this logic:
1/10 = 0.000110011b = .099609375d

.099609375d x 10d = 0.960937d != 1d
It can be the largest possible representation of 0.1 without being 0.1*

You are also limited in the amount of bits of which can be stored for the fraction itself, within the floating point structure**.
Posted Image


You may wish to work only in integers, or pennies as long as your cost is not abnormally large (xxxxxxxx.xx).
int a = 190;

int b = 210;

float answer = (a+b) / 100.[B]0; [/B]//or 100.0f for floating point division. Integer division (6 / 4) may equal 0

Alexander.

*A possible option for correcting fraction rounding errors: Machine epsilon - Wikipedia, the free encyclopedia
**A reference to the data type I was discussing: Single-precision floating-point format - Wikipedia, the free encyclopedia

So I should only use integers throughout my program?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

fightinillini94 said:

So I should only use integers throughout my program?
If you are using binary floating points, then you should try to prepare for error especially when different systems may treat floats differently due to architecture (not good when money is involved). Working in cents (as integers) is intuitive and completes the solution without adding complexity, and that may be the best possible solution for the task.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users