Hi folks. I have written a code. I got 4 data (int) in a txt file: an operator, tho numbers from an interval and a power.
For example: + 2 5 2
I want a FOR cycle to do this:
Power all members of the interval and add them like this:
res=2^2+3^2+4^2+5^2=54
My code is:
Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
int num1, num2, kitevo, res;
char op;
int i;
FILE* fp = fopen ("be.txt", "r");
while (fscanf(fp, "%c%d%d%d\n", &op, &num1, &num2, &kitevo) == 4) {
switch (op)
{
case '+' :
for (int i = num1; i <= num2; i++);
{
res += pow(i, kitevo);
}
case '*' :
for (int i = num1; i <= num2; i++);
{
res *= pow(i, kitevo);
}
case '-' :
for (int i = num1; i <= num2; i++);
{
res = 0-(res += pow(i, kitevo));
}
break;
}
FILE* outfile = fopen("ki.txt", "w");
fprintf(outfile, "%d\n", res);
}
return 0;
}
It gives me wrong result, but i dont see any mistake.
Thanks in advance!