Creating a banker's program in beginner's C class. All of my code works successfully (after testing all cases), except for forcing a return of negative one in part of the loop. When the value is returned by copy to the main program it alters the balance.
Function return
else { //If the amount to be withdrawn from the account is greater than the existing amount
printf("Error. Withdrawal must be less than account balance.\n"); //Output error message
return -1; //Return a negative one to the main program
}
Return by copy in main
case 3: //Cash Withdrawal
printf("You are about to withdraw cash from an account.\n \n");
withdrawnAmount = withdrawal(balance); //Calling function to withdraw money from existing account
balance -= withdrawnAmount;
printf("Your new account balance is $%d\n\n", balance);
break;
I think in that case you should either return 0;
so that the amount will not be deducted, like this:
else { //If the amount to be withdrawn from the account is greater than the existing amount
printf("Error. Withdrawal must be less than account balance.\n"); //Output error message
return 0; //Return zero to the main program
}
Or, if you want to keep using the negative one, you would need to check the value returned before deducting from the account balance, as follows:
case 3: //Cash Withdrawal
printf("You are about to withdraw cash from an account.\n \n");
withdrawnAmount = withdrawal(balance); //Calling function to withdraw money from existing account
if(withdrawnAmount > 0)
balance -= withdrawnAmount;
else
printf("-1");
printf("Your new account balance is $%d\n\n", balance);
break;
Hope it helps.