Hi everyone,
This is my first post on here. I am currently following a c programming introduction course (I don't have any experience in programming). I am programming in Kate on Linux (virtual machine).
I am currently working on making a basic calculator. I am getting there, I am only missing a function to convert a 16 bit binary imput to decimal value, doing research on how to do that best now.
However, I am getting something wierd when I execute the program. There is a � sign before "Result: x". Sometimes it is not a � but a '.' or '/'. I am not sure how it can be removed. Any help would be great!
PS.
I know that this program can be written in fewer lines, however my priority is on making a working program first.
Code:
#include <stdio.h>
int main(void)
{
int b, x;
float res, a;
char c, bord;
res = 0;
printf("** Calculator **\n");
while (c != '=')
{
printf("Binary or decimal? (b/d) ");
scanf(" %c", &bord);
switch(bord)
{
case 'd': printf("Value? ");
scanf(" %f", &a);
printf("Input: %.0f\n", a);
printf("Operation? ");
scanf(" %c", &c);
printf(" %c", &c);
switch(c)
{
case '+': res = a + res;
break;
case '-': res = res - a;
break;
case '/': res = res / a;
break;
case '*': res = a * res;
break;
case 'c': res = 0;
break;
default: res = res;
break;
}
break;
//Make a function for converting binary values
/*case 'b': printf("Value? ");
scanf(" %f", &a);
x = read_binary_value();
printf("Input: %f.0\n", x);
printf("Operation? ");
scanf(" %c", &c);
printf(" %c", &c);
switch(c)
{
case '+': res = x + res;
break;
case '-': res = res - x;
break;
case '/': res = res / x;
break;
case '*': res = x * res;
break;
case 'c': res = 0;
break;
default: res = res;
break;
}
break;*/
default: a = 0;
printf("Operation? ");
scanf(" %c", &c);
printf(" %c", &c);
switch(c)
{
case '+': res = a + res;
break;
case '-': res = res - a;
break;
case '/': res = res / a;
break;
case '*': res = a * res;
break;
case 'c': res = 0;
break;
default: res = res;
break;
}
break;
}
printf("Result = %.2f\n", res);
}
}