1. Well I included the fclose's and moved the (y == 0) to the top. I actually got a result this time and half of it is what I want but the other half isn't so it's still fair to say that progress is being made. The infile is only a short list of _ + _ or _ - _ etc: with a non-operator and a divide by zero thrown in. I'll include the new code and results and the input file contents will be obvious to you then....... Thks Greatly

Code:
```#include <stdio.h>

int main()
{
FILE* infile;
FILE* outfile;
int x, y, answer;
char op;

infile = fopen("calc4.in", "r");
outfile = fopen("calc4.out", "w");

do
{

fscanf(infile, "%d %c %d", &x, &op, &y);

if (y == 0)
fprintf(outfile, "\nDivision by zero not allowed!");
else
if (op == '+')
answer = x + y;
else
if (op == '-')
answer = x - y;
else
if (op == '*')
answer = x * y;
else
if (op == '/')
answer = x / y;
else
fprintf(outfile, "\nIllegal Operator.");

fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);

}
while (!((x == 0) && (y == 0)));

fclose(infile);
fclose(outfile);

return 0;
}```
Result :
Code:
```5+6 = 11
4-2 = 2
Illegal Operator.
8x7 = 2
6*5 = 30
64/16 = 4
Illegal Operator.
5t6 = 4
Division by zero not allowed!
1/0 = 4
Division by zero not allowed!
0+0 = 4```

2. ## This should do ...

1. You need to check
if (y == 0 && operator == '\')
for skipping the calculation instead of just y == 0, as you still want to do the calculation when y is 0 but the operator is +, - or *

2. fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
This should be executed only when calculation is done i.e. when the operator is illegal or if it is divided by zero, do not execute this line. So you need to decide where to put this line in the if else statement. I am sure you can sort this out : ) .

3. Thanks for all the help. I am learning much by this problem and I know it helps with the future ones. I'll try your suggestions and see what it does .......thks agin!

4. I would put it here:
Code:
```    if (op == '+')
{
answer = x + y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
}
else
/*etcetera etcetera */```
Hope this helps!!

5. Good Morning! (at least it is here)
I have changed the code yet again and while it certainly is looking good it won't even give a result now (just the error message). I don't know much yet about how to read the debug info but one thing I do notice is that when you cycle through the " if (op == '+') etc: when it goes through the loop the third time the program keeps the value of the answer for the '-' when it should be divide. The list that the program reads from
Code:
```
5 + 6
4 - 2
8 x 7
6 * 5
64 / 16
5 t 6
1 / 0
0 + 0```
so it shows 8*7 as equaling 2.
Here's what I have now:
[code]
#include <stdio.h>

int main()
{
FILE* infile;
FILE* outfile;
int x, y, answer;
char op;

infile = fopen("calc4.in", "r");
outfile = fopen("calc4.out", "w");

do
{

fscanf(infile, "%d %c %d", &x, &op, &y);

if (y == 0)
fprintf(outfile, "\nDivision by zero not allowed!");

if (op == '+')
answer = x + y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
if (op == '-')
answer = x - y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
if (op == '*')
answer = x * y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
if (op == '/')
answer = x / y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);

if (op != '+')
fprintf(outfile, "\nIllegal Operator.");
if (op != '-')
fprintf(outfile, "\nIllegal Operator.");
if (op != '*')
fprintf(outfile, "\nIllegal Operator.");
if (op != '/')
fprintf(outfile, "\nIllegal Operator.");

}
while (!((x == 0) && (y == 0)));

fclose(infile);
fclose(outfile);

return 0;
}

Thanks greatly for all help and advice to everyone concerned. Hope the time comes when I can be of service too.

6. You need the brackets after the if statement for a start, ie:
Code:
```if(op == '+')
{  /*Need to enclose the following two lines in braces*/
answer = x + y;   /*as they are individual to each if statement*/
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
}```

7. Thanks ......... I'm starting to get pretty blurry on this file now. I should have known about the braces but that still does not allow my program to execute. I keep going over it in the debugger but most of the resulting dialog is still a foreign language to me. I will however keep plugging away cause in the end I will "Understand"!

8. Hmmm! I don't think I understand what you mean. The previous calculation was 4-2 with the result being 2 then when the loop repeats it should read 8*7 with result being 15. I am trying to make anything but "+,-,*,/" be an illegal expression and also any attempt to divide by 0 result in an error message but continue if the list warrants. I also want it to stop when x and y both = 0. I have to be honest , I am quite perplexed........... thks

9. 1. As I suggested before, you need to check for
op == '/' when you check for y == 0. Otherwise, it will always skip the calculation when y is 0, even when the operator is +, - etc.

2. In your data file, it is 8 x 7 as the other suggested, not 8 * 7, which is different. And in your program, there is no 'x' operator, only '*' operator, so it will say illegal operator.

3. When the operator is illegal and no calculation is done, the value of 'answer' will not be changed, so it will keep whatever it was from last calculation e.g. when it does 4-2 = 2, so answer = 2. And now it try to do 8 x 7, but because operator 'x' is illegal, it will not carry out the calculation, and 'answer' remains 2. So when you print it out, it remains 2.

4. You have changed all the else if statements to if statements. the program will therefore keep printing the illegal operator message.

You need to be very careful and think about when and how to use if - else if statements and what conditions to check. It will be good if you run your program in your head first or better yet, use breakpoints to step through the code and see why it is doing the strange things.

Hope this helps.

10. Thanks guys!
I finally got it. You were both right about the 8x7 not being 8*7 of course and all your help was useful and appreciated. I am on to another set of exersizes now so I may have to enlist your help and expertise again. I thought I would include my code so you can see how I managed to do it. Thanks again!

Code:
```#include <stdio.h>

int main()
{
FILE* infile;
FILE* outfile;
int x, y, answer;
char op;

infile = fopen("calc4.in", "r");
outfile = fopen("calc4.out", "w");

do
{
fscanf(infile, "%d %c %d", &x, &op, &y);

/* check for division by 0 */

if ((y == 0) && (op == '/'))
fprintf(outfile, "\nDivision by zero not allowed!");

/*check for legal operator */

else if (op == '+'){
answer = x + y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
} else if (op == '-'){
answer = x - y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
} else if (op == '*'){
answer = x * y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
} else if (op == '/'){
answer = x / y;
fprintf(outfile, "\n%d%c%d = %d", x, op, y, answer);
}
/* check for illegal operator */
else
fprintf(outfile, "\nIllegal Operator!");

}
while (!((x == 0) && (y == 0)));

fclose(infile);
fclose(outfile);

return 0;
}```

Popular pages Recent additions