-2

I made this calculator. The problem I'm experiencing is that, when it repeats after giving an answer, it prints this line twice. Why is it doing so, and how do I make it print once?

printf("\n\n==========================="); 
printf("\n\nEnter operator: ");

#include<stdio.h>
#include<conio.h>
int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
int div(int a, int b);

    void main(){
        int a;
        int b;
        char c;
        do{
        printf("Calculator by Maisam, If u want to exit the program press q");
        printf("\n\n===========================");
        printf("\n\nEnter operator: ");
        scanf("%c",&c);
        switch(c){
            case '+':
                printf("\n> Enter first number: ");
                scanf("%d",&a);
                printf("\n> Enter second number: ");
                scanf("%d",&b);
                printf("\n===========================");
                printf("\n\n< Answer is %d\n",add(a,b));
                break;
            case '-':
                printf("\n> Enter first number: ");
                scanf("%d",&a);
                printf("\n> Enter second number: ");
                scanf("%d",&b);
                printf("\n===========================");
                printf("\n\n< Answer is %d\n",sub(a,b));
                break;
            case '*':
                printf("\n> Enter first number: ");
                scanf("%d",&a);
                printf("\n> Enter second number: ");
                scanf("%d",&b);
                printf("\n===========================");
                printf("\n\n< Answer is %d\n",mul(a,b));
                break;


            case '/':
                printf("\n> Enter first number: ");
                scanf("%d",&a);
                printf("\n> Enter second number: ");
                scanf("%d",&b);
                printf("\n===========================");
                printf("\n\n< Answer is %d\n",div(a,b));
                break;

            }
        }
        while(c != 'q');
    }


    int add(int a, int b){
        return a+b;
    }

    int sub(int a, int b){
        return a-b;
    }

    int mul(int a, int b){
        return a*b;
    }

    int div(int a, int b){
        return a/b;
    }
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
ramb0
  • 127
  • 1
  • 11
  • 2
    `scanf("%c",&c);` -> `scanf(" %c",&c);` skip space character. – BLUEPIXY Sep 01 '14 at 01:26
  • possible duplicate of [Second scanf is not working](http://stackoverflow.com/questions/4023643/second-scanf-is-not-working) – chux - Reinstate Monica Sep 01 '14 at 01:30
  • 2
    [Never use scanf](https://stackoverflow.com/questions/24302160/scanf-on-an-istream-object/24318630#24318630). – zwol Sep 01 '14 at 01:40
  • Adding a screen shot will likely reduce interest in the post as the the result is difficult to read and not search-able. Use @BLUEPIXY solution and see if the problem remains. OR - Add `printf("%d\n",c);` right after your `scanf("%c",&c);` to see that `scanf()` is reading your "Enter" also. – chux - Reinstate Monica Sep 01 '14 at 02:05
  • Try `c = getch();` at the place of `scanf("%c",&c);` – Himanshu Sep 01 '14 at 05:41

2 Answers2

1

This is because scanf leaves \n after you input anything because you press the enter key after entering any data.

Adding a getchar() after the scanf should fix the problem. Or adding a space before %c will do

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

Whenever you press enter after giving the input, this is consumed by your scanf which is accepting a char (as pressing enter also results into char)

Adding a space before %c will solve your problem.

A much cleaner and better code with your problem solved :

#include<stdio.h>
#include<conio.h>

int add(int a, int b);
int sub(int a, int b);
int mul(int a, int b);
int div(int a, int b);

    int main()
    {
        int a;
        int b;
        char c;
        do
        {
          printf("Calculator by Maisam, If u want to exit the program press q");
          printf("\n\n===========================");
          printf("\n\nEnter operator: ");
          scanf(" %c",&c); // skip a space character

          // Accept the numbers and then operate on them according to operator
          printf("\n> Enter first number: ");
          scanf("%d",&a);
          printf("\n> Enter second number: ");
          scanf("%d",&b);
          printf("\n===========================");


          switch(c){
            case '+':
                printf("\n\n< Answer is %d\n",add(a,b));
                break;
            case '-':
                printf("\n\n< Answer is %d\n",sub(a,b));
                break;
            case '*':
                printf("\n\n< Answer is %d\n",mul(a,b));
                break;
            case '/':
                printf("\n\n< Answer is %d\n",div(a,b));
                break;
            default :
                printf("Invalid choice");

            }
        } while(c != 'q');
      return 0;
    }


    int add(int a, int b){
        return a+b;
    }

    int sub(int a, int b){
        return a-b;
    }

    int mul(int a, int b){
        return a*b;
    }

    int div(int a, int b){
        return a/b;
    }
Sagar D
  • 2,588
  • 1
  • 18
  • 31