I have to write a simple calculator in C, that only uses getchar/putchar. I need to echo the user input, and then perform the mathematical operation (only need to use +, /, %, *, and -). Right now, I only care about what I'm doing wrong, as far as storing the values into my input variables. The rest of the program should be fairly easy to do myself.
In my while loop, I tried to be careful with nesting if/else's (mainly for error-checking purposes) only inside my "else if's". I want my loop to ignore spaces, and then assign the number, math operator, and other number into input1, input2, and input3, respectively. Right now, if I input something like "55 * 66", I am returned something like "*0".
Thanks for looking.
UPDATE (3/22/2014): I'm getting a little bit closer. My problem right now is that the program will only work when I input one space after each number and operand (i.e. "2 + 4" works, but anything without spaces, or with more than one space, does not). I also didn't quite get how to putchar the numbers to output them. I used printf so I could at least have a working program, in the mean time. Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
int add(int input1,char operand, int input2);
int subtract(int input1,char operand, int input2);
int mod(int input1,char operand, int input2);
int multiply(int input1,char operand, int input2);
int divide(int input1,char operand, int input2);
int main()
{
int answer = 0;
int ch = 0;
int input1 = 0;
char operand = 0;
int input2 = 0;
int function = 0;
printf("\nPlease enter a calculation to be made.\n");
while (((ch = getchar()) != ' ') && (ch != '\n')){
if (ch == '-') {
printf("\nError: no negatives allowed.\n");
}
else if (!isdigit(ch)){
printf("\nError: number not inputted (first number).\n");
}
else {
input1 = (input1 * 10) + (ch - '0');
}
}
while (((ch = getchar()) != ' ') && (ch != '\n')){
switch(ch){
case '+':
operand = '+';
break;
case '-':
operand = '-';
break;
case '%':
operand = '%';
break;
case '*':
operand = '*';
break;
case '/':
operand = '/';
break;
default:
printf("Error: input is not one of the allowed operands.");
break;
}
}
while (((ch = getchar()) != ' ') && (ch != '\n')){
if (ch == '-') {
printf("\nError: no negatives allowed.\n");
}
else if (!isdigit(ch)){
printf("\nError: number not inputted (second number).\n");
}
else {
input2 = (input2 * 10) + (ch - '0');
}
}
printf("%d", input1);
putchar(' ');
printf("%c", operand);
putchar(' ');
printf("%d", input2);
putchar(' ');
putchar('=');
putchar(' ');
if (operand == '+'){
answer = add(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '-'){
answer = subtract(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '%'){
answer = mod(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '*'){
answer = multiply(input1, operand, input2);
printf("%d", answer);
}
else if (operand == '/'){
answer = divide(input1, operand, input2);
printf("%d", answer);
}
return 0;
}
int add(int input1,char operand, int input2){
return input1 + input2;
}
int subtract(int input1,char operand, int input2){
return input1 - input2;
}
int mod(int input1,char operand, int input2){
return input1 % input2;
}
int multiply(int input1,char operand, int input2){
return input1 * input2;
}
int divide(int input1,char operand, int input2){
return input1/input2;
}