ATM bug

Hi all,

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).

How should I structure my code to fix this bug?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
using namespace std;

int main ()

{

long int o = 20; //declaring $20 variable
long int p = 50; //declaring $50 variable
long int q; //multiples of $20
long 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: $"; <<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
}
Last edited on
Hi,

130 is not a multiple of 50, so line 25 fails.

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 long int ? 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 :+)

Good Luck
Thanks for the reply.

I'll make the necessary changes and see how I go.
I'm thinking of subtracting 10 in the formula so that I can calculate an input which isn't a multiple of 50 but am unsure how to go about it.

This is my updated code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
using namespace 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";
  else if (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
}
What is the maximum multiple of 50 one can subtract from the amount so that the remainder is a multiple of 20?
Well it would be unlimited because the userInput is unknown. It could be 50 or it could 2000.
Last edited on
Well it would be unlimited because the userInput is unknown. It could be 50 or it could 2000.


So you haven't really thought about it, have you.

You could develop a methodology that works regardless of the numbers.

I am trying to get you to think about what you are doing - as in problem solving.

This is applied mathematics - develop a methodology using variables.

It's the same problem as this:

Apples are $5, oranges $2 - you have 20$ to spend, you have to buy some of each - what is the maximum number of Apples you will get?

More importantly what is a general solution to this - that is the spend amount can vary?
Topic archived. No new replies allowed.