2

I am creating a very basic calculator in C but the output is not coming as desired.

#include<stdio.h>
int main(int argc, char const *argv[])
{
    /* code */
    char ch;
    int a,b,p=0;
    scanf("%d",&a);
    while(1)
    {
        ch=getchar();
        if(ch==';')
        {
            p=2;
            break;
        }
        scanf("%d",&b);
        if(ch=='+')
        {
            a+=b;
        }
        if(ch=='-')
        {
            a-=b;
        }
        if(ch=='*')
        {
            a*=b;
        }
        if(ch=='/' && b!=0)
        {
            a/=b;
        }
        if(ch=='/' && b==0)
        {
            printf("INVALID INPUT\n");
            p=2;
            break;
        }
    }

    if(p!=0)
        printf("%d",a);
    return 0;
}

The Output is always coming as the initial value which has been assigned to "a".

Output which is coming-

4
+
5
;
4

Expected output -

4
+
5
;
9

Can you please help me with this issue of why the expression is not getting evaluated correctly?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Learner
  • 35
  • 4
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Dec 26 '21 at 05:12
  • Are you typing `4`, `+`, `5`, `;` with each character on a separate line? If so, that's the input rather than the output. – Jonathan Leffler Dec 26 '21 at 05:15
  • @JonathanLeffler: Lines 1-4 are input, Line 5 is output. – Andreas Wenzel Dec 26 '21 at 05:16
  • @AndreasWenzel — yes, but it is not what the question says. – Jonathan Leffler Dec 26 '21 at 05:17
  • After you type `4` and newline, what is the value in `ch`? It is a newline. So you attempt to read a number, but `+` is not a number. You need to check that you get a good value from each call to `scanf()` by testing the value returned by `scanf()`, and you need to check what is in `ch` too. – Jonathan Leffler Dec 26 '21 at 05:20
  • Actually I am entering first I/P, then pressing the 'enter' entering the character. Then again pressing enter, I entered the second I/P. – Learner Dec 26 '21 at 05:20

2 Answers2

3

The line

scanf("%d",&a);

will consume the first number from the input stream, but it will leave the newline character on the input stream.

Therefore, when you later call

ch=getchar();

it will read that newline character. It will not read the + character.

If you want to read the + character, then you can change that line to the following:

scanf( " %c", &ch );

This line will first discard all whitespace characters and will match the first non-whitespace character on the input stream.

Afterwards, your program will have the desired output:

4
+
5 
;
9

An alternative solution would be to discard the rest of the line after every call to scanf that uses the %d format specifier. That way, calling getchar immediately afterwards should read the + character as intended.

You can discard the remainder of an input line using the following code:

//discard remainder of input line
{
    int c;

    do
    {
        c = getchar();

    } while ( c != EOF && c != '\n' );
}

Here is a more compact way of writing the same thing:

//discard remainder of input line
for ( int c; (c=getchar()) != EOF && c != '\n'; )
    ;
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

The only problem is in scanning the inputs.
As a tab or a new line must seperate the value supplied to scanf.

In short, just add \n at the end of scanf.

scanf("%d\n", &a);
scanf("%d\n", &b);

this should do it.

git_gud
  • 649
  • 1
  • 11
  • 27