Reputation: 31
Can anyone help me out with the following c program? This is only my second program in c so I have no clue what I'm doing.
I'm trying to create a program in c to read a file "input.txt" and give students' math scores in asterisks based on a student's percent of correct answers
all I'm coming up with is an infinite loop of
+:*
-:*
*:*
/:*
input.txt would be similar to below
1 number of students
Karla students name
8 10 addition score decided by 8/10
50 80 subtraction score decided by 50/80
30 60 multiplication score decided by 30/60
2 5 division score decided by 2/5
with a desired output of
Karla
+: * * * * * * * *
-: * * * * * *
*: * * * * *
/: * * * *
my code
int num;
char name;
int sum;
int cur;
int i;
int avg;
//call file
FILE*ifp=fopen("input.txt","r");
fscanf(ifp,"%d",&num);
for (i=0;i=num;i++){
fscanf(ifp,"%c",&name);
printf("%c",&name);
for(sum=0;sum<=4;sum++){
fscanf(ifp,"%d",&cur);
scanf(cur,"%d %d")
avg=%d/%d;
if (sum==1)
printf("+:");
else if (sum==2)
printf("-:");
else if (sum==3)
printf("*:");
else if (sum==4)
printf("/:");
if (avg==1)
printf("**********\n");
else if (avg>=.9)
printf("*********\n");
else if (avg>=.8)
printf("********\n");
else if (avg>=.7)
printf("*******\n");
else if (avg>=.6)
printf("******\n");
else if (avg>=.5)
printf("*****\n");
else if (avg>=.4)
printf("****\n");
else if (avg>=.3)
printf("***\n");
else if (avg>=.2)
printf("**\n");
else if (avg>=.1)
printf("*\n");
else
printf("\n");
}
}
//close file
fclose(ifp);
return 0;
If anyone could help me out i would greatly appreciate it.
Upvotes: 1
Views: 118
Reputation: 3034
Consider the following For Loop:
for(i = 0, i < 10; i++)
{
DoSomething();
}
The order that this executes is:
In your case, instead of checking if i is equal to num, you're actually assigning num to i. Because C treats 0 as "False" and anything else as "True", and because the "result" of i = 10 is 10 (see sidebar1) the computer evaluates i = 10 as "True" so the loop keep going
This is a pretty easy mistake to make in C because the compiler won't warn you about it. Static analysis tools like Lint will, but that's another story.
In Conclusion, you probably want "i = num" to be "i < num" or "i <= num" or some other conditional statement.
Sidebar1 This property allows you to chain assigns like a = b = c = 10. The result of c = 10 is 10 so that gets assigned to b. The result of b = 10 is 10 so that gets assigned to a.
Upvotes: 0
Reputation: 4194
Your infinite loop is because of your outer loop conditional:
change for (i=0;i=num;i++)
to for (i=0;i<num;i++)
As it is, you're just assigning i to num each time, hence the infinite loop.
EDIT: I should also point out that you have a syntax error on your lines
scanf(cur,"%d %d")
avg=%d/%d;
And that your avg
variable is declared as an int, and as such you'll be performing integer division, which likely won't act as you expect; that is, you'll only get whole numbers, so your subsequent conditionals will be largely meaningless. You should make it into a float or double and cast one of your operands to be floating point
Upvotes: 2