First there are a few issues with the code here. Number 1, why do you declare operand as a char array instead of a single char?
You then use it invalidly in the scanf call.
Change
Code:
char operand[1];
...
...
scanf("%lf %c %lf", &num1, operand, &num2);
to
Code:
char operand;
...
...
scanf("%lf %c %lf", &num1, &operand, &num2);
And then you will need to change all your conditions to be just if (operand == '*') etc.. instead of operand[0]. Not sure what your logic was there..
Second issue is the use of calling fflush() on
stdin this is a BIG no-no as it causes
undefined behavior. Google it if you don't know what it is.
Code:
/* Waiting to cause Segfault/random error (undefined behavior) */
fflush(stdin);
What you need to do is use something better then scanf() but as a quick fix you can put a space in front of your calls. So you can do:
Code:
scanf(" %c", &exitsw);
This is a little trick that will make scanf() ignore leading whitespace (stray newline characters for example) in the input stream. You should
really be using a better form of input capture some combination of fgets()/sscanf or something of that nature.
Finally, as far as your question on input validation--You can fix this easily by inverting the logic on your condition:
Code:
}while (exitsw != 'q' && exitsw != 'Q');