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);
}