Please check the accuracy of my code (yes or no statements, withdrawal)

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.

This is my 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
using namespace 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;
        }
        else if(choice != 'N'&& choice != 'n'&&choice != 'Y'&&choice != 'y'){
            cout<<"Invalid input."<<endl;
            valid = true;
        }
    }
    
}

Last edited on
You should use int since you are only dispensing whole numbers
Last edited on
You should really use code tags:

[code]
//Your Code
[/code]



If you want to loop it, simply do this:

1
2
3
4
5
6
7
8
9
10
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.
Last edited on
@zaphshe


Thank you!

Could you show me where to put the loop and code tags?
Last edited on
Thank you! fixed!
Could you show me where to put the loop

Loop is simply in int main() where I showed you.
1
2
3
4
5
    // Test the Remainder Amount if it is even
    int remE;
    remE = remainderAmt % 20;
    
    if (remE >= 10 || remE <= 20 )
that's not what even means
not sure what you are trying to do there


1
2
            cout<<"Invalid input."<<endl;
            valid = true;
so valid means invalid


also, I don't see a main() function
Topic archived. No new replies allowed.