Hello! I was hoping someone could just check over my code. I am trying to create a program that lets a user input a numerical value and then have the program output the correct change. Then have them asked if they would like to do another transaction Y/N.
#include <iostream>
usingnamespace std;
void Function( ){
beginning:
int main = 0;
long inputAmt;
cout << "This machine only will dispense in $1, $5, $10, $20, and $50 bills only" << endl;
do
{
cout << "Enter in the amount to be withdraw : " << endl;
cin >> inputAmt;
if (inputAmt < 1.00) cout << "Please input a number greater than $1.00";
if (inputAmt > 300.00) cout << "Please input a number less than or equal to $300.00";
}while(inputAmt < 1 || inputAmt > 300);
// Determines the amount of $50 bills to dispense
long fiftyBill;
fiftyBill = inputAmt/50;
long remainderAmt;
remainderAmt = inputAmt - (fiftyBill*50);
// Test the Remainder Amount if it is even
int remE;
remE = remainderAmt % 20;
if (remE >= 10 || remE <= 20 )
{
// If it is not, reduced $50 by 1 and add it to the remainder amount
fiftyBill = fiftyBill - 1;
remainderAmt = remainderAmt + 50;
}
// Determines the amount of $20 bills to dispense
long twentyBill;
twentyBill = remainderAmt / 20;
//Determine the number of $10 dollar bills to dispense
long tenBill;
tenBill = remainderAmt / 10;
//Determine the number of $5 dollar bills to dispense
long fivebill;
fivebill = remainderAmt / 5;
//Determines the number of $1 bills to dispense
long dollarBill;
dollarBill = remainderAmt / 1;
// Output Message
cout << "Number of $50 dollar bills : " << fiftyBill << endl;
cout << "Number of $20 dollar bills : " << twentyBill << endl << endl;
cout << "Number of $10 dollar bills : " << tenBill << endl << endl;
cout << "Number of $5 dollar bills : " << fivebill << endl << endl;
cout << "Number of $1 dollar bills : " << dollarBill << endl << endl;
cout << "Number of bills dispensed is " << fiftyBill + twentyBill + tenBill + fivebill + dollarBill << endl;
int valid = 0;
int choice;
while(valid){
cout<<"Would like to do another withdrawal?(Y/N)"<<endl;
cin >> choice;
if(choice =='N' || choice =='n'){
break;
}
if(choice =='Y'||choice =='y'){
goto beginning;
}
elseif(choice != 'N'&& choice != 'n'&&choice != 'Y'&&choice != 'y'){
cout<<"Invalid input."<<endl;
valid = true;
}
}
}
int main()
{
char ask;
do
{
Function();
std::cout << "Do You Want Another Transaction: Y/N";
std::cin >> ask;
} while (ask == 'y' || ask == 'Y');
}
Your program is not functional - it gives way more bills than it should. I don't even know what logic you were trying to use to dispense the change. You can instead try to work it out with remainders:
1 2 3 4 5
if(inputAmt/50 >= 1)
std::cout << "Number of $50 dollar bills : " << int(inputAmt/50);
//Will have the amount of change left to dispense
int AmountLeft = inputAmt - 50;
Something like that may be easier. But as it is now, your code doesn't work with any tested input.