1,105,371 Community Members

Why is my switch statement running twice?

Member Avatar
andrewpw
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
int OperateGumBallMachine(void){

	int total = 0;
	char input = getchar();

	RefillGumBallMachine();
	for(input; input != 'x'; input = getchar()){
		if(gMachine.iNumBlueGumBalls + gMachine.iNumGreenGumBalls + gMachine.iNumRedGumBalls == 0){
			RefillGumBallMachine();
		}
		switch(input){
			case 'n':
				total += NICKLE;
				if(total >= QUARTER){
					total -= QUARTER;
					DeliverGumBall();
					printf("Your change is %d cents\n", total);
					total = 0;
				}
			break;
			case 'd':
				total += DIME;
				if(total >= QUARTER){
					total -= QUARTER;
					DeliverGumBall();
					printf("Your change is %d cents\n", total);
					total = 0;
				}
			break;
			case 'q':
				total += QUARTER;
				if(total >= QUARTER){
					total -= QUARTER;
					DeliverGumBall();
					printf("Your change is %d cents\n", total);
					total = 0;
				}
			break;
			default:
				printf("Whoops you've entered the wrong command, please try again.");
			break;
		}
		printf("You've deposited %d cents\n", total);
	}

	return(0);
}

This is the function I am having trouble with. Every time I run my program, and enter a character, my program prints the correct output (for example if I enter 'n' the first time, my program outputs "you've deposited 5 cents"), but then it also outputs the default message every time ("Whoops you've entered the wrong command, please try again."). Why is it printing this?
If I put this statement before the switch statement:
printf("%d\n", input)
to see what input the switch statement is getting, it shows 'n' and then the rest of the output, followed by a blank line and the the default statement again, as though it is running through the switch statement a second time with nothing entered for the input variable.
If I remove the default case for the switch statement it prints "You've deposited 5 cents" two times.
What is wrong with my code? Please help.

Member Avatar
DJSAN10
Posting Whiz in Training
260 posts since Dec 2010
Reputation Points: 28 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
0
 

input = getchar()

Might be causing the problem...!!!! ? Although i am not quite sure

Member Avatar
deceptikon
Eternally Awesome
4,691 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

Every time I run my program, and enter a character, my program prints the correct output (for example if I enter 'n' the first time, my program outputs "you've deposited 5 cents"), but then it also outputs the default message every time ("Whoops you've entered the wrong command, please try again."). Why is it printing this?

Look very closely at what keys you press when you enter a character. Say it's 'n' the first time. Don't you also hit the enter key? That's the character your program is reading the second time.

Member Avatar
DJSAN10
Posting Whiz in Training
260 posts since Dec 2010
Reputation Points: 28 [?]
Q&As Helped to Solve: 27 [?]
Skill Endorsements: 0 [?]
 
0
 

Okay.. that is your problem. It reads newline as second input after you enter 'n' and thus enters default . Maybe you could check for getchar() != EOF

EDIT : deceptikon and I posted at the same time :P :D

Member Avatar
Gaiety
Junior Poster
135 posts since Sep 2009
Reputation Points: 1 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 1 [?]
 
0
 

The simplest i can think is to include the below

for(input;input!='x';input=getchar()) {
    if (!isspace(input)){
       switch(input) {
        ......
        ......
       }
    } 
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: