Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:40:29+00:00 2026-06-14T12:40:29+00:00

Using a very simple calculator program that prompts a user for an operation to

  • 0

Using a very simple calculator program that prompts a user for an operation to perform, followed by a prompt for two integers on which to perform this operation. The program is supposed to loop after these operations, except in the case where the user enters the character ‘q’, at which point the program is supposed to quit.

#include <stdio.h>

int main (void)
    {
        char c;
        int number[2], num1, num2, result;
        double num1d, num2d, resultd;
        int done=1;

        while(done)
        {
        printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
        c = getchar();
        printf("\tplease enter a number \n");
        scanf("%d",&number[0]);
        printf("\tplease enter another number \n");
        scanf("%d",&number[1]);
        num1 = number[0];
        num2 = number[1];

        switch(c)
            {
            case('-'):
            result = num1-num2;
            printf("\nThe first number you entered subtracted by the second number is %d.\n", result);
            break;

            case('+'):
            result = num1+num2;
            printf("The first number you entered added to the second number is %d.\n", result);
            break;

            case('*'):
            result = num1*num2;
            printf("The first number you entered multiplied with the second number is %d.\n", result);
            break;

            case('/'):
            num1d = (double) num1;
            num2d = (double) num2;
            resultd = num1d/num2d;
            printf("The first number you entered divided by the second number is %g.\n", resultd);;
            break;

            case('q'):
            printf(" Now Exiting...\n");
            done=0;
            break;

            default:
            puts("Invalid key pressed. Press q to exit");
            break;
            }
        }

        return 0;
    }

Works correctly for a single calculation, but subsequently performs oddly; in particular it prints

printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
printf("\tplease enter a number \n");

altogether.

The standard method of clearing the input buffer while (getchar() != '\n'); doesn’t fix this. One out of two times that this text displays incorrectly the user can still use the program as if the instructions were displaying as they should (so the user can type an operation such as +, carriage return, and then some integer and a carriage return, and the program will perform correctly from that point on) Every other time however the program will put “Invalid key pressed. Press q to exit” regardless of input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:40:30+00:00Added an answer on June 14, 2026 at 12:40 pm

    What everyone else here is saying is true, getchar() returns an int but that’s not your problem.

    The problem is that getchar() leaves a newline character after you use it. If you’re going to use getchar() you must always consume the newline char afterwards. This simple fix:

       printf("\t What sort of operation would you like to perform? \n \t Type + - * / accordingly. \n");
        c = getchar();
        getchar();     //<-- here we do an extra getchar for the \n
        printf("\tplease enter a number \n");
        scanf("%d",&number[0]);
        printf("\tplease enter another number \n");
        scanf("%d",&number[1]);
    

    and that will eliminate the problem. Every time you type <somechar><enter> it’s really putting two characters on the buffer, for example if I hit + and enter I’m getting:

    '+''\n'  // [+][\n]
    

    getchar() will only get the first of these, then when getchar() is called again it won’t wait for your input it will just take that '\n' and move on to the scanf()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I built a very simple addition calculator in python: #This program will add two
Here's a very simple program using the function: #include <windows.h> #include <tchar.h> #include <atlstr.h>
I'm trying to write a very simple program in C++ that finds the modulus
I am trying to build a very simple C++ program using the Maven NAR
I am working on a very simple app that will let user to pick
I am using a very simple structure, mapping, as defined below: struct mapping{ int
I'm using a very simple Stylesheet Switch by php. It was fine all along
I am using flask , and trying to do something very simple using the
riting a very simple search using like and have an option to omit options,
It is very simple to do paging on access database (using absolute and pagesize)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.