As you can tell I'm new to C++ and I need some help.
The below function needs a user to ask for a dollar amount, print out whether that amount can be dispensed and if it can, print out the number of 50s and number of 20s to be dispensed.
The below code works however I get the cout "Please enter the withdrawl amount in multiples of $20 and $50" when I put in amount such as $130 which is wrong. Obviously you can withdraw this amount (1 x $50 note and 4 x $20 notes).
#include <iostream>
usingnamespace std;
int main ()
{
longint o = 20; //declaring $20 variable
longint p = 50; //declaring $50 variable
longint q; //multiples of $20
longint r; //multiples of $50
int userInput; //declaring withdrawl amount variable
cout << "***********************************************************" <<endl;
cout << "~~~~~~~~~~Welcome to the Federal Reserve Bank ATM~~~~~~~~~~" <<endl;
cout << "***********************************************************" <<endl;
cout << endl;
cout << "Please enter the dollar amount you wish to withdraw: $"; <<endl;
cin >> userInput; //waits for user input
q = (userInput % p) / o; //determines multiple of $20
r = userInput / p; //determines multiple of $50
if (userInput % p == 0 || userInput % o == 0 || (userInput % p) % o == 0) //determines valid user input
cout << "Please take your withdrawl amount of " << r << " $50 note/s and " << q << " $20 note/s";
else
cout << "Please enter the withdrawl amount in multiples of $20 and $50" <<endl;
return 0; //terminating the program
}
You will need a cunning plan if you wish to solve this. Write out the steps necessary to do this, put them in as comments in the code, then write the code to do what is specified in the comment.
Choose better variable names p q r are not good choices.
Why are you using longint ? Do you realise that they can be negative? Google to see what the maximum size of this type is. Hint you must have very rich atm customers :+)
#include <iostream>
usingnamespace std;
int main ()
{
int o = 20; //declaring $20 variable
int p = 50; //declaring $50 variable
int q; //multiples of $20
int r; //multiples of $50
int userInput; //declaring withdrawl amount variable
cout << "***********************************************************" << endl;
cout << "~~~~~~~~~~Welcome to the Federal Reserve Bank ATM~~~~~~~~~~" << endl;
cout << "***********************************************************" << endl;
cout << endl;
cout << "Please enter the dollar amount you wish to withdraw: $";
cin >> userInput; //waits for user input
q = (userInput % p) / o; //determines multiple of $20
r = userInput / p; //determines multiple of $50
if (userInput % p == 0 || userInput % o == 0 || (userInput % p) % o == 0) //determines valid user input
cout << "Please take your withdrawl amount of " << r << " $50 note/s and " << q << " $20 note/s";
elseif (userInput % o == 0 || userInput % p == 0 || (userInput % o) % p == 0)
cout << "Please take your withdrawl amount of " << q << " $50 note/s and " << r << " $20 note/s";
else
cout << "Please enter the withdrawl amount in multiples of $20 and $50" << endl;
return 0; //terminating the program
}