Instead of this:
Code:
while (EOF != fscanf(cmd, "%c %f %f", &op, &num1, &num2))
{
float result;
switch (op)
{
case '+':
result = num1 + num2;
break;
/* ... */
case 'Q':
printf("\nThank you for using Calculator (Version 1.0)... Goodbye\n");
goto cleanupArea;
/* ... */
}
printf("\n%4.2f %c %4.2f = %4.2f \n", num1, op, num2, result);
fflush(stdout);
}
I am suggesting something like:
Code:
#define LINE_MAXLENGTH 80
/* ... */
char line[LINE_MAXLENGTH + 1];
while (fgets(line, LINE_MAXLENGTH + 1, cmd))
{
float result;
line[strcspn(line, "\n")] = '\0'; /* remove trailing newline, if any */
if (strcmp(line, "DA") == 0)
{
/* ignore or do whatever DA is supposed to mean */
}
else if (strcmp(line, "Q") == 0)
{
printf("\nThank you for using Calculator (Version 1.0)... Goodbye\n");
goto cleanupArea;
}
/* ... check for other special commands ... */
else if (sscanf(line, "%c %f %f", &op, &num1, &num2) == 3)
{
switch (op)
{
case '+':
result = num1 + num2;
break;
/* ... */
}
printf("\n%4.2f %c %4.2f = %4.2f \n", num1, op, num2, result);
fflush(stdout);
}
else
{
/* handle input error */
}
}