I just started programming in C and I am using Code Blocks to learn. I was working on a simple ATM program and decided to use a goto function to use when an invalid entry is entered. When I 1st used it, it ran as expected. But now it won't move past one of the statements. Code is below.
When any of the options 1-3 is pressed, it runs as supposed to, but also proceeds to run the selection error section after it. If I just try to run the selection error portion, it goes through it, and keeps repeating it over and over. How do I stop this from happening? I only need invalid selection portion to run when the conditions are met and only then. Thank you!
int iSelection = 0;
float fTransAmount = 0.0;
float fBalance = 100.25;
printf("\n\n\tATM\n");
menu_options:
printf("\n1\t To Make a Deposit Press One");
printf("\n2\t To Make a Withdrawal Press Two");
printf("\n3\t To End Transaction, Press Three\n");
scanf("%d", &iSelection);
if (iSelection == 1) {
printf("\n Enter Amount to Deposit: ");
scanf("%f", &fTransAmount);
printf("\n Your new balance is: $%.2f", fBalance + fTransAmount);
} //End if for 1
if (iSelection == 2) {
printf("\n Enter Amount to Withdraw: ");
scanf("%f", &fTransAmount);
if (fTransAmount > fBalance)
printf("\n Insufficient funds, ending transaction.....\n");
else
printf("\n Your new balance is $%.2f\n", fBalance - fTransAmount);
} //End if for 2
if (iSelection == 3) {
printf("\n ending transaction");
} //End if for 3
if (iSelection != 1 || iSelection != 2 || iSelection != 3 ) {
printf("\nInvalid selection, please try again");
goto menu_options;
} //End if for Selection Error