Hello, I'm making a simple C calculator using only getchar and putchar functions, my program works only if the arithmetic is entered correctly but if it's not, I can't make the proper error checking on it if the arithmetic is enteres improperly.
EXAMPLE:
4 + 7 = 11 =====> Works!
7 * -3 = -21 =====> Works!
4 [2spaces] + [3spaces] 7 =====> Won't error check.
% 5 =====> Won't error check.
4 * =====> Won't error check.
Any suggestions please? Thanks!
/*
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
void error (int), add (int, int, bool, bool),
subtract (int, int, bool, bool),
multiply (int, int, bool, bool),
mod (int, int, bool, bool),
divide (int, int, bool, bool);
int main(void)
{
int ch = 0, first = 0, second = 0, op = 0, next = 0;
bool firstIsNeg = false, secondIsNeg = false;
char operand = 0;
printf ("Enter an arithmetic query on this format: [int] [operand] [int]\n");
while (((ch = getchar()) != ' ') && (ch != '\n'))
{
if (ch == '-'){
firstIsNeg = true;
ch = 0;
}
else if (!isdigit (ch))
error(1);
else if (ch == ' ') next = 1;
else
first = first * 10 + (ch - '0');
}
while (((ch = getchar()) != ' ') && (ch != '\n') && (next = 1))
{
if (ch == 43) operand = '+';
if (ch == 45) operand = '-';
if (ch == 47) operand = '/';
if (ch == 42) operand = '*';
if (ch == 37) operand = '%';
}
ch = 0;
while (((ch = getchar()) != ' ') && (ch != '\n'))
{
if (ch == '-'){
secondIsNeg = true;
ch = 0;
}
else if (!isdigit (ch))
error(2);
else
second = second * 10 + (ch - '0');
}
if (operand == '+') add (first, second, firstIsNeg, secondIsNeg);
if (operand == '-') subtract (first, second, firstIsNeg, secondIsNeg);
if (operand == '/') divide (first, second, firstIsNeg, secondIsNeg);
if (operand == '*') multiply (first, second, firstIsNeg, secondIsNeg);
if (operand == '%') mod (first, second, firstIsNeg, secondIsNeg);
}
void error (int error){
char choice;
if (error == 1) printf ("ERROR: Non-digit in input at 1st premise. Program terminated.\n\n");
if (error == 2) printf ("ERROR: Illegal operand. Program terminated.\n\n");
if (error == 3) printf ("ERROR: Non-digit in input at 2nd premise. Program terminated.\n");
printf ("Press any key to enter another calculation, or press Q to quit program.\n\n");
scanf ("%c", &choice);
if (choice == 'Q' || choice == 'q') exit (1);
else main ();
}
void add (int first, int second, bool firstIsNeg, bool secondIsNeg){
char choice;
if (firstIsNeg == true) first *= -1;
if (secondIsNeg == true) second *= -1;
printf ("\n%d + %d = %d\n\n", first, second, first + second);
fflush(stdin);
printf ("Press any key to enter another calculation, or press Q to quit program.\n");
scanf ("%c", &choice);
if (choice == 'Q' || choice == 'q') exit (1);
else main ();
}
void subtract (int first, int second, bool firstIsNeg, bool secondIsNeg){
char choice;
if (firstIsNeg == true) first *= -1;
if (secondIsNeg == true) second *= -1;
printf ("\n%d - %d = %d\n\n", first, second, first - second);
fflush(stdin);
printf ("Press any key to enter another calculation, or press Q to quit program.\n");
scanf ("%c", &choice);
if (choice == 'Q' || choice == 'q') exit (1);
else main ();
}
void multiply (int first, int second, bool firstIsNeg, bool secondIsNeg){
char choice;
if (firstIsNeg == true) first *= -1;
if (secondIsNeg == true) second *= -1;
printf ("\n%d x %d = %d\n\n", first, second, first * second);
fflush(stdin);
printf ("Press any key to enter another calculation, or press Q to quit program.\n");
scanf ("%c", &choice);
if (choice == 'Q' || choice == 'q') exit (1);
else main ();
}
void mod (int first, int second, bool firstIsNeg, bool secondIsNeg){
char choice, operand = '%';
if (firstIsNeg == true) first *= -1;
if (secondIsNeg == true) second *= -1;
printf ("\n%d %c %d = %d\n\n", first, operand, second, first % second);
fflush(stdin);
printf ("Press any key to enter another calculation, or press Q to quit program.\n");
scanf ("%c", &choice);
if (choice == 'Q' || choice == 'q') exit (1);
else main ();
}
void divide (int first, int second, bool firstIsNeg, bool secondIsNeg){
char choice;
if (firstIsNeg == true) first *= -1;
if (secondIsNeg == true) second *= -1;
printf ("\n%d / %d = %.1f\n\n", first, second, 1.0 * first / second);
fflush(stdin);
printf ("Press any key to enter another calculation, or press Q to quit program.\n");
scanf ("%c", &choice);
if (choice == 'Q' || choice == 'q') exit (1);
else main ();
}