write a program that will do a basic transaction of atms in c++. It will decrease the amount of something by how many times it is used

StackOverflow https://stackoverflow.com/questions/23315906

Question

I have the part that asks you to enter an amount and it will output what type of notes it must be.(e.g for $220 the output will be 2 $100 and 2 $10.)

I have already did the calculation part mentioned above. I need the function that can store how many hundred etc notes are found in the machine and it must decrease each time they are used. (e.g given above example if there were 10 notes of each, after transaction only 8 notes of each will be left.)

The program must also ask if the user wants to make another transaction after each trans. and if there is not enough notes to do the trans. the program must exit by saying not enough funds. so far i got (i will add the other notes after i am complete with the storing):

void Transaction(int dollars, int& thousands, int& hundreds)
{
 thousands=(dollars/1000);
dollars-=thousands*1000;
 hundreds=(dollars/100);
dollars-=hundreds*20;
 }
 int main(void)
 {
 int dollars; 
 int thousands=0;
 int hundreds=0;

 cout<<"Enter the amount:";
 cin>> dollars;

 Transaction(dollars,thousands,hundreds);

 cout<<"$1000 Notes:"<< thousands <<endl;
 cout<<"$100 Notes:"<< hundreds <<endl;
 }
Was it helpful?

Solution

#include<iostream>
#include <cstdlib>
using namespace std;
void Transaction(int dollars, int& thousands, int& hundreds, int& p_thousands, int& p_hundreds)
{
  if(p_thousands*1000 + p_hundreds*100 > dollars)
  {
 thousands=(dollars/1000);

  dollars-=thousands*1000;

  hundreds=(dollars/100);

  dollars-=hundreds*20;
  }
  else
  {
  cout << "Not Enough Funds" <<endl;
  exit(0);
  }

  }
 int main(void)
 {
 int dollars; 
 int p_thousands=10;
 int p_hundreds=10;
 int thousands=0;
 int hundreds=0;
 char c;
do
{
cout<<"Enter the amount:";
cin>> dollars;
Transaction(dollars,thousands,hundreds,p_thousands,p_hundreds);
p_thousands  -= thousands;
p_hundreds -= hundreds;
cout<<"$1000 Notes:"<< thousands <<endl;
cout<<"$100 Notes:"<< hundreds <<endl;
// for debugging purpose
//cout<<"$1000 Notes present:" <<p_thousands <<endl;
//cout<<"$100 Notes present:" <<p_hundreds <<endl;
cout<<"Press y for another transaction" <<endl;
cin>>c;
}while(c=='y');


}

OTHER TIPS

#include<iostream>
#include <cstdlib>
using namespace std;
void Transaction(int dollars, int& thousands, int& hundreds, int& p_thousands, int&     p_hundreds)
{
if(p_thousands*1000 + p_hundreds*100 >= dollars)
{
  if(dollars/1000>p_thousands)
  {
  dollars -=p_thousands*1000;
  thousands =p_thousands;
  }
  else
  {
  thousands=(dollars/1000);
  dollars-=thousands*1000;
  }
  if(dollars/100>p_hundreds)
  {
  dollars -=p_hundreds*100;
  hundreds =p_hundreds;
  }
  else
  {
  hundreds=(dollars/100);
  dollars-=hundreds*20;
  }
}
else
{
cout << "Not Enough Funds" <<endl;
exit(0);
}

}
int main(void)
{
int dollars; 
int p_thousands=10;
int p_hundreds=10;
int thousands=0;
int hundreds=0;
char c;
do
{
cout<<"Enter the amount:";
cin>> dollars;
Transaction(dollars,thousands,hundreds,p_thousands,p_hundreds);
p_thousands  -= thousands;
p_hundreds -= hundreds;
cout<<"$1000 Notes:"<< thousands <<endl;
cout<<"$100 Notes:"<< hundreds <<endl;
// for debugging purpose
cout<<"$1000 Notes present:" <<p_thousands <<endl;
cout<<"$100 Notes present:" <<p_hundreds <<endl;
cout<<"Press y for another transaction" <<endl;
cin>>c;
}while(c=='y');


}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top