Hey, everyone, I am trying to create a code that outputs a certain amount of money but can't be less than 1.00 or more than 300 dollars. For some reason, it's not stopping at those numbers during the test run it is still letting me input any amount.
#include <iostream>
usingnamespace std;
int main()
{
long inputAmt;
cout << "This machine only will dispense in $1, $5, $10, $20, and $50 bills only" << endl;
cout << "Enter in the amount to be withdraw : " << endl;
cin >> inputAmt;
if (inputAmt==.99) cout << "please input a number greater than $1.00";
if (inputAmt==301) cout << "Please input a number greater than or equal to $300.00";
// 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 dollars 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;
{
}}
if (inputAmt==.99) cout << "please input a number greater than $1.00";
if (inputAmt==301) cout << "Please input a number greater than or equal to $300.00";
Line 13 should be
if (inputAmt < 1.00) cout << "please input a number greater than $1.00";
and line 14 should be..
if (inputAmt > 300.00) cout << "Please input a number less than or equal to $300.00";
You're also not reducing the variable 'remainderAmt' after finding the quantity of 20's, 10's etc.
EDIT..
Lastly, you should use a do/while around the input to be sure a valid amount was entered.
ie>>
1 2 3 4 5 6 7 8 9
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);