This program is supposed to read commands from a file in sequential order, process them, perform the necessary calculations, and then print out the results in a neat, readable manner, both to a file and to the screen. I only have to implement commands: +, -, *, /, H, Q.
I got it to scan H but not sure how to get Q. Also I'm not sure if it is reading the file or how to write it to another file. What can I do?
#include <stdio.h>
#include <stdlib.h>
Code:
int main(void) {
char H, Q, choice, operate;
float i, j;
FILE *file1;
printf("Commands available: +, -, *, or / \n");
printf("Press H for help or Q for quit \n");
fflush(stdout);
scanf("%c",&choice);
if (choice = H)
printf("Commands available: +, -, *, or / \n");
else
{
if (choice = Q)
printf("Program Ended \n");
}
file1= fopen("CommandProj1.dat", "r");
if (file1 == NULL)
printf("Error opening input file 1 \n");
else
{
fscanf(file1, "%c %f %f", &operate, &i, &j);
if (operate == '+')
printf("%f\n", i + j);
if (operate == '-')
printf("%f\n)", i - j);
if (operate == '*')
printf("%f\n", i * j);
if (operate == '/')
printf("%f\n", i / j);
}
fclose (file1);
return 0;
}
Oh and the data file gives these numbers
GN
+ 34 43
+ -34 43
+ -4 -71
- 27 15
- -4 -71
H
* 3 -5
* -11 -12
/ 3 14
/ 14 3
/ 14 -3
Q
H