-2

I tried a simple calculator exercise. Even though I got the logic right. I was struggling to find why I couldn't read a character after two integers in it.

Not working code given below (getting the numbers first then the operation as character). But in the same code if I read the character first then the numbers it is working all good. Not getting the logic behind this :(

#include <stdio.h>
#include <stdint.h>

void add (unsigned int temp1, unsigned int temp0);
void subtract (unsigned int temp1, unsigned int temp0);
void multiply (unsigned int *temp);
void divide (unsigned int *temp);

int main()
{
    unsigned int num[2];
    char op;

    printf("Enter two numbers\n");
    scanf("%u", &num[0]);
    scanf("%u", &num[1]);
   
    printf("Enter operation\n");
    scanf("%c", &op);

    switch(op)
    {
        case '+': add(num[0],num[1]);
        break;

        case '-': subtract(num[0],num[1]);
        break;

        case '*': multiply(num);
        break;

        case '/': divide(num);
        break;

        default: printf("Invalid Operation");
        break;
    }

    return 0;
}


void add (unsigned int temp1, unsigned int temp0)
{
    printf("Sum of the numbers is: %u", temp1+temp0);
}

void multiply (unsigned int *temp)
{
    printf("Product of the numbers is: %u", temp[0]*temp[1]);
}

void subtract (unsigned int temp1, unsigned int temp0)
{
    if (temp1 > temp0)
    {
        printf("Difference of the numbers is: %u", temp1-temp0);
    }
    else
    {
        printf("Difference of the numbers is: %u", temp0-temp1);
    }
}

void divide (unsigned int *temp)
{
    unsigned int quo;
    unsigned int rem;
    
    quo = temp[0]/temp[1];
    rem = temp[0]%temp[1];
    
    printf("Quotient of %u divided by %u is: %u\n", temp[0], temp[1], quo);
    printf("Remainder of %u divided by %u is: %u\n", temp[0], temp[1], rem);
}
  • 1
    You should provide sample input and actual vs. expected output/results. – jarmod Jun 16 '22 at 15:28
  • xing Thanks it works. But why is it not a problem when the character was read first. Like this. printf("Enter operation\n"); scanf("%c", &op); printf("Enter two numbers\n"); scanf("%u", &num[0]); scanf("%u", &num[1]); – Karthick Duraisamy Jun 16 '22 at 15:35
  • `scanf("%c", &op);` will read a character without skipping initial whitespace no matter when you call it. If you input a space before the real operation character then `op` will be set to the space character. – Ian Abbott Jun 16 '22 at 15:54
  • Does this answer your question? [scanf() leaves the newline character in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-newline-character-in-the-buffer) – Oka Jun 16 '22 at 18:54
  • Thanks for all the feedback. Now I am able to understand where I went wrong. Able to correct now. – Karthick Duraisamy Jun 18 '22 at 17:15

1 Answers1

0

We are just guessing as we don't exactly know what you want to achieve. If you want to skip reading the new line character, then you can use the suggestion of Ian Abbot.

By making the following change:

scanf("%c", &op); // Change this line
scanf(" %c", &op); // To this

This should solve your problem.

Adrian
  • 468
  • 2
  • 6
  • 15