C Program - Basic Calculator

13,584

Solution 1

You need to consume the trailling newline, enter remains in the stdin buffer ready to be read by the next scanf.

Change

scanf("%c", &yes);

to

scanf(" %c", &yes);

Solution 2

The problem is that the newline character is left on the input after inputting a number to choose what operation to do. So when the user is asked if they want to do it again, the newline is taken instead of the y or n. I might be wrong, it's been a while since I've done some programming. What worked for me to fix it was to put a little bit of code after the line

printf("\nAgain (Y/N): ");

Adding this bit right after the printf statement will remove the newline from the input and should do the trick.

while(getchar() != '\n')
   getchar();

Maybe someone else can explain exactly why this works, I don't remember the specifics. It's a little thing that I found useful to remember, it comes up every now and again.

Share:
13,584

Related videos on Youtube

Delfino
Author by

Delfino

Programming and gaming enthusiast that's constantly looking to tackle new, challenging projects. As a gamer I mainly play Magic: The Gathering and Monster Hunter, but have been and always been a fanboy of all things Nintendo, especially Zelda :) Being the self-proclaimed lifelong learner that I am, I thoroughly enjoy all that this site has to offer, and never seem to tire of reading all the questions people post on various topics! As far as programming languages are concerned, I'm quite well-versed in the ways of Java, as most classes I took in college focused that. Despite the narrow-mindedness of my University, I've branched out and have taken a liking to C and Python.

Updated on June 04, 2022

Comments

  • Delfino
    Delfino over 1 year

    I'm trying to write a basic calculator program in C, and I'm almost there! I have one issue though, and it's concerning repeatedly querying the user for input.

    I can get through my loop once, but even though the user inputs the correct character, my program still breaks out of the loop.

    I'm fairly new to C, but I've done a decent amount of programming in Java, so I understand the functionality of loops, conditionals, and data types.

    #include <stdio.h>
    
    int main()
    {
        char yes;
        int a, b, c, choice;
    
        yes = 'y';
        while(yes == 'y' || yes == 'Y')
        {
            printf("Enter first integer: ");
            scanf("%d", &a);
    
            printf("Enter second integer: ");
            scanf("%d", &b);
    
            printf("\nAdd(1), Subtract(2), Multiply(3), Divide(4): ");
            scanf("%d", &choice);
    
            printf("\n");
            switch(choice) 
            {
                    case(1):
                        c = a + b;
                        printf("%d + %d = %d\n", a, b, c);
                        break;
                    case(2):
                        c = a - b;
                        printf("%d - %d = %d\n", a, b, c);
                        break;
                    case(3):
                        c = a * b;
                        printf("%d * %d = %d\n", a, b, c);
                        break;
                    case(4):
                        c = a / (float)b;
                        printf("%d / %d = %d\n", a, b, c);
                        break;
                    default:
                        printf("Incorrect choice. Try again.\n");
            }
    
            printf("\nAgain (Y/N): ");
            scanf("%c", &yes);
        }
    
        return 0;
    }
    
  • erip
    erip over 8 years
    When a user types return, there is a \n introduced. Adding a space swallows that.
  • chux - Reinstate Monica
    chux - Reinstate Monica over 8 years
    Note: while(getchar() != '\n') is an infinite loop should getchar() return EOF. Best to test for both EOF and '\n'.