Basically, I have to build a program that reads a mathematical command (*, /, +, -) and two variables in a text file and performs the command with the two variables and prints the answer to the screen and to another text file.
i wrote the code and can only see !!!Hello World!!!
Code:
#include
<stdio.h>
#include
<stdlib.h>
int main(void)
{
File *input;
File *output;
char initials[2], command[1];
double var1, var2;
//Open the input file to read from
input = fopen("input.txt", "r");
//Open the output file to write to
output = fopen("output.txt", "w");
//Make sure files open
if(input && output != NULL)
{
fscanf(input,'%s %d %d',command, &var1, &var2);
if(command == "Q"){exit(0);}
else if(command == "+")
{
printf("%d + %d = (var1+var2)\n");
fprintf(output, "%d + %d = (var1+var2)\n");
}
else if(command == "-")
{printf("%d - %d = (var1-var2)\n");
fprintf(output, "%d - %d = (var1-var2)\n");
}
else if(command == "*")
{printf("%d X %d = (var1*var2)\n");
fprintf(output, "%d X %d = (var1*var2)\n");
}
else if(command == "/")
{printf("%d / %d = (var1/var2)\n");
fprintf(output, "%d / %d = (var1/var2)\n");
}
else if(command == H)
{printf("Q = Quit\n/ divides\n* multiplies\n+ adds\n- subtracts\nH = Help");
fprintf(output, "Q = Quit\n/ divides\n* multiplies\n+ adds\n- subtracts\nH = Help");
}
else
{printf("Something went wrong.\n");
fprintf(output, "Something went wrong.\n");}
}else
{printf("Could not open one of the files.\n");
fprintf(output, "Could not open one of the files.\n");