Please help me with my C code?

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.

1 Answer

Relevance
  • 9 years ago
    Favorite Answer

    I just did a quick scan over the code and it looks okay except for the use of getchar().

    With stdio, at least on some operating systems like Windows Console applications, when you trigger getting some input from the user like that the operating system gets a whole line and then returns the first character. So let's say the program just started up and executes getchar(). The operating system (Windows) will fetch an entire line until the ENTER key is hit, then return the first character. Now you see it wait and you type in "n" followed by the ENTER key. Windows records and remembers TWO characters for you: The n and a '\n' which is for the ENTER key. Your loop ends with using getchar() again, which then fetches the ENTER key's '\n' into the input variable (without reprompting you, yet) and your algorithm then complains.

    You need to get an entire line and just look at the first character, or else if you use getchar() you need to deal with eating up characters until it sees the '\n' and then skips over it.

    Try this -- I've added three lines at the end of your loop:

    int OperateGumBallMachine(void) {

        int total = 0;

        char input;

            RefillGumBallMachine();

            for ( input = getchar(); 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);

                do

                    input = getchar();

                while ( input != '\n' );

            }

        return(0);

    }

Still have questions? Get your answers by asking now.