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

First you need to fix many things you are doing completely wrong.
1) fflush(stdin) is an error -- follow the link
2) Never NEVER call main() . main() is not a recursive function. Create a loop instead.
3) The syntax

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

is quite confusing. Each should have its own definition and its own line.

Thanks so much, now I have another question, im still sorting the blank spaces but for some reason it's not doing what's it supposed to.

my goal is to ignore all spaces and just save the int, but the first digit is always iterated. THanks again.

SAMPLE
Enter an arithmetic query in this format: [INT] [OPERAND] [INT]
[2 spaces] 12 [2 spaces] 34
2 and 4
Press ENTER for another calculation, or press Q to quit program.

printf ("Enter an arithmetic query in this format: [INT] [OPERAND] [INT]\n");

		while (((ch = getchar()) == ' ') && (ch != '\n')) {
				blank++;
			}
		while (((ch = getchar()) != ' ') && (ch != '\n')) {
				if (my_isDigit(ch)) {
					first = first * 10 + (ch - '0');
				}
			}
		while (((ch = getchar()) == ' ') && (ch != '\n')) {
				blank++;
			}
		while (((ch = getchar()) != ' ') && (ch != '\n')) {
				//if (!my_isDigit(ch)) error (1);
				second = second * 10 + (ch - '0');
			}
			
		printf ("%d and %d\n", first, second);

my goal is to ignore all spaces and just save the int, but the first digit is always iterated. THanks again.

Huh? That statement makes no sense. Use words you understand, not new ones you're not sure of [iterated?]. Digits are not iterated.

I think I figured out what you meant to say. The first digit is thrown away. Right?

Lets assume you entered " ..123..543 (. is SPACE)

printf ("Enter an arithmetic query in this format: [INT] [OPERAND] [INT]\n");

		while (((ch = getchar()) == ' ') && (ch != '\n')) {
				blank++;

This loop reads:
SPACE
SPACE
1

The 1 is not a SPACE so the loop exits.

while (((ch = getchar()) != ' ') && (ch != '\n')) {
				if (my_isDigit(ch)) {
					first = first * 10 + (ch - '0');
				}
			}

This loop reads
2, calculates first as 2 (hopefully first is set to 0 somewhere.
3, calculates first as 23
SPACE, not a digit, the loop exits.
You can figure out the rest.

Now this is something you could have figured out simply by sitting at your desk with pencil and paper and running through the code yourself. It's so much faster to desk-check your own work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.