You are here:

C++/please help me out

Advertisement


Question
hey vijay thank u for answering the previous quesktion?

i have got one more quesktion?
i trying to create the system like the piggy bank.
but it is showing me wrong calcualtion.
if it is possible to change long in double and do the calcualtion
i tryden with double it wasnt working


#include <iostream>

using namespace std;

void main()
{
  // Programmer: Mike Gozdiskowski
  // Date: 9/13/2002
  // Discussion: An interactive program to calculate the change
  // from a vending machine

  // Input: Cost of item purchased (an integer between 0 and 100 inclusive)
  // Output: Change is Twohundert, Hundert, fifty, and cents

  // Declaration and description of variables
  // Declaration and description of variables
  long cost;   // Cost of item purchased in cents
  long change;   // Amount of change in cents
  long Twohundert;   // Number of Twohundert in change
  long Hundert;  //Number of Hundert in change
  long fifty;  //Number of fifty in change
  long twenty;  //Number of twenty in change
  long thk;
  long tempchange;
  long ten;
  long five;
  long two;
  long one;


  // Welcome user and get amount of purchase
  cout<<"Welcome to the cash register!"<<endl;
  cout<<"Please enter the amount of your purchase: ";
  cin>>cost;
  cout<<"Please enter the amount of change: ";
  cin>>thk;
  // Calculate the total change due
  change=(thk-cost);

  
  Twohundert=change/200;
  Hundert=(change%200)/100;
  fifty=(change%100)/50;
  twenty=(change%50)/20;
  ten=(change%20)/10;
  five=(change%10)/5;
  two=(change%5)/2;
  one=(change%2)/1;


  /


  
  cout<<"\t\t"<<"Total change due: "<<change<<" cents"<<endl;
  cout<<"\t\t"<<"Your change is"<<endl;
  cout<<"\t\t"<<Twohundert<<"\t"<<(char)156<<"2 Coin's"<<endl;
  cout<<"\t\t"<<Hundert<<"\t"<<(char)156<<"1 Coin's"<<endl;
  cout<<"\t\t"<<fifty<< "\t50p"<<endl;
  cout<<"\t\t"<<twenty<< "\t20p"<<endl;
  cout<<"\t\t"<<ten<< "\t10p"<<endl;
  cout<<"\t\t"<<five<< "\t5p"<<endl;
  cout<<"\t\t"<<two<< "\t2p"<<endl;
  cout<<"\t\t"<<one<< "\t1p"<<endl;

  tempchange = change;
while (tempchange > 0)
{
  if (tempchange > 200)
   {tempchange = tempchange-200;
   Twohundert=Twohundert+1;}

  else if (tempchange > 100)
   {tempchange = tempchange-100;
   Hundert=Hundert+1;}

  else if (tempchange > 50)      
   {tempchange = tempchange-50;
   fifty=fifty+1;}

  else if (tempchange > 20)
   {tempchange = tempchange-20;
   twenty=twenty+1;}

  else if (tempchange > 10)
   {tempchange = tempchange-10;
   ten=ten+1;}

  else if (tempchange > 5)      
   {tempchange = tempchange-5;
   five=five+1;}

  else if (tempchange > 2)
   {tempchange = tempchange-2;
   two=two+1;}

  else if (tempchange > 1)
   {tempchange = tempchange-1;
   one=one+1;}
}

}

Answer
Suppose the change in cents is 534.

534 / 200 == 2 is the number of 200 cent coins

534 % 200 == 134 is the remainder

134 / 100 == 1 is the number of 100 cent coins

134 % 100 == 34 is the remainder

34 / 20 == 1 is the number of 20 cent coins

34 % 20 == 14 is the remainder

and so on


         int change_in_cents ;

         // ...

         // compute change_in_cents

         int _200 = change_in_cents / 200 ;
         change_in_cents %= 200 ;

         int _100 = change_in_cents / 100 ;
         change_in_cents %= 100 ;

         int _50 = change_in_cents / 50 ;
         change_in_cents %= 50 ;

         // and so on

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.