Please help to find out why my various "a" gives unexpected result

***Hello, i a newbie in Swift programming. I tried to create a simulator of ATM ,but stuck when tried in below way. Dont need best solution, just to correction. ***

import Foundation
var operatingScreen: Bool = true
while operatingScreen == true {

var balance: Int = 500               //initial balance

func showBalance(){
print("Your current balance is \(balance) $")}

showBalance()

print("""
Please choose a transaction
1 for Income
2 for Cashout
3 for Exit
""")
if let input = readLine() {
if let x = Int(input) {
    
switch x {
   case 1 :
    print("Please enter amount")
    if let input = readLine() {
    if let amount = Int(input) {    // Amount entrance
        balance += amount
        print("+ \(amount)")}}
    showBalance()
        
case 2:
        print("Please enter amount")
        if let input = readLine() {
        if let amount = Int(input) {
            
            if amount < 5 {
                print("Your amount should be more than 5$ to proceed Cash out ")
                break}
            if amount > balance {
                print("Amount exceeds balance")
                break}
            
                balance = balance - amount
            
        print("""
        Please choose an available bank note:
        100
        50
        20
        10
        5
        """)
            
        if let input = readLine() {
            if let banknote = Int(input) {
      

=======================
This "a" takes unexpected values.However it is empty so it have to take 0 by default. So it doest work proper in further

 var a: Int 
                   var ost: Int //   remains

                   a = amount / banknote
                   ost = amount-a*banknote
            
                   func cashout() {
                   for _ in 1...a {
                       print( "\(banknote)")
                   }}
                    
                    func countEdenici() {
                        var edenica: Int // units
                            if ost > 5 {
                            edenica = ost - 5
                                print("5 \n")
                                for _ in 1...edenica {
                                    print("1 \n")
                                }
                                if ost == 5 {
                                    print("5 \n")
                                }
                            if ost < 5 {
                                for _ in 1...edenica {
                                    print("1 \n")}
                                }}}
                                
                   
                

The rest of code i cut to make my question shorter.

===================================================================

In my other tries i used different ways to get a good result. Now i tried to use more Functions, but stuck in thread.

This is false. a is initialized to amount / banknote here:

a = amount / banknote

It is not initially set to 0 unless the result of the above expression happens to be 0.

Please help to find out why my various “a” gives unexpected result

When asking questions about unexpected behavior, you should always explicitly state what behavior you expect, because it may be different from what other people expect. In this case, I have no idea what you expect.

I don't know what you consider a "good result".

Terms of Service

Privacy Policy

Cookie Policy