eagergrammer eagergrammer - 1 year ago 53
C Question

Function(s) are being improperly executed/repeated within a switch statement. Rest of code doesn't execute

So far

executes fine and the
loop executes fine. If I put a wrong PIN in, program execution immediately stops. If otherwise, the program proceeds to through the rest of its execution.

I decided to test one function,
and that's where I have problems. The transfer function only executes to the printf statements correctly. Instead of the program waiting for my input of which account to do the transfer on, it jumps back to the beginning of
and keeps repeating…

The switch statement in
doesn't even execute! Why?

//This program will mimick an ATM machine
//It will do deposits, withdrawals, and authentication

//Function Declarations
//float withdrawal();
//float deposit();
float transfer ();
void decision ();

float checking = 15050.00;
float savings = 8200.83;

#include <stdio.h>

int main ()
int PIN;
printf ("Welcome to Wells Fargo 24HR Teller!\n");
printf ("Enter Your 4-Digit PIN \n");
scanf ("%d", &PIN);

while (PIN == 2016) {
decision (); //this starts the ATM choices user can select
if (PIN != 2016) {
return 0;

void decision ()

printf ("I want to:\n");
printf ("A) Make a Transfer\n");
//printf("B) Make a Deposit\n");
//printf("C) Make a Withdrawal\n");
//printf("D) Exit\n");

char selection;
scanf ("%c", &selection);

switch (selection) {
case 'A':
transfer ();
//case 'B': deposit();
//case 'C': withdrawal();
//case 'D': break;

float transfer ()
float transamt;
float chekbal;
float savbal;
printf ("Transfer Amount? \n");
scanf ("%f", &transamt);
printf ("\n Transfer Amount of %f", transamt);

printf ("\n Which Account? \n");
printf (" A) Checking \n");
printf (" B) Savings \n");

/*Right after this point, execution goes back to beginning of decision()
program execution doesn't go through switch statement */
char confirm;
scanf ("%c", &confirm);

switch (confirm) {
case 'A':
chekbal = (transamt + checking);
printf ("Checking Balance is Now %f", chekbal);

case 'B':
savbal = (transamt + savings);
printf ("Savings Balance is Now %f", savbal);

return 0;

Answer Source

The problem is that scanf with the %c conversion specifier does not consume whitespace, see here:

All conversion specifiers other than [, c, and n consume and discard all leading whitespace characters

Pressing enter after inputting a transfer amount inserts a newline into stdin, which scanf("%c", &confirm) then sees and stores in confirm, that is, confirm now contains a newline. To see this explicitly, try adding case '\n': to the switch(confirm) statement; you'll see that the switch statement is in fact being executed, it's just that the value of confirm didn't match any of your previous cases. It's good practice to always add a default: label to switch statements in order to catch any unexpected values.

The solution is very simple, you just need to add any whitespace character before %c in the format string, since (from the same source above),

any single whitespace character in the format string consumes all available consecutive whitespace characters from the input

So, scanf(" %c", &confirm); will first get rid of any whitespace in stdin (the leftover newline in our case) before reading the character that will be assigned to confirm.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download