-
Help Needed
Hi folks, this has got me stumped.
input: (from a file called ch7d) Output:
1 0 student ssessment attempt report
1 0
1 1 group 1
1 1 none 2
1 2 one 2
5 2 both 1
5 2 total 5
5 1
4 2 group 5
4 2 both 2
4 0 one 1
4 1 total 3
2 2
99 group 4
both 2
none 1
one 1
total 4
group 2
both 1
total 1
basically the figure on the left of the two relates to a student group number, the figure
on the right is the number of exams each student sat. The spec for the code is:
open and read the files
WHILE not at the end of the data(99)
group total = 0
store group as current group
output group heading
WHILE still processing current group and not at the end of the data
store attempts as current attempts
attempt toal = 0
WHILE still processing current attempts and still processing current group and not
at the end of the data
increment attempt total
read the next line.
IF current attempt is 2
output "both"
ELSE IF current attempt is 1
output "one"
ELSE
output "none"
output attempt total
accumulate group total
output group total
My code so far is:
#include<stdio.h>
#include<conio.h>
main()
{
int group,
attempt,
group_total,
current_attempt,
current_group,
attempt_total;
FILE *ch7d = fopen("ch7d.dat", "r");
fscanf(ch7d, "%d %d", &group, &attempt);
printf("Student Assessment Attempt Report\n");
while (group <= 99)
{
{
group_total = 0;
current_group = group;
printf("Group %d\n", current_group);
}
while (group = current_group && group <= 99)
{
current_attempt = attempt;
attempt_total = 0;
}
/*stuck from here*/
while ((attempt = current_attempt) && (group = current_group) && (group <= 99))
{
attempt_total += 1;
fscanf(ch7d, "\n%d %d", &group, &attempt);
}
if (current_attempt == 2)
{
printf("\nBoth ");
}
if else (current_attempt == 1)
{
printf("\nOne ");
}
else
{
printf("\nNone ");
}
printf("%d", attempt_total)
group_total???????
fclose(ch7d);
getch();
}
The program seems to get stuck on the 3rd while condition. The 2nd while appears fine as
I stuck a printf in there.
Anyone any ideas?? PLEASE!?!?!
:confused:
-
Remember the = sign means assignment and == means equality.
//Change to == here like this
while (group == current_group && group <= 99)
{
current_attempt = attempt;
attempt_total = 0;
}
/*stuck from here*/
//Same change needed here
while ((attempt == current_attempt) && (group = current_group) && (group <= 99))
{
attempt_total += 1;
fscanf(ch7d, "\n%d %d", &group, &attempt);
}
-
I missed one on this line:
while ((attempt == current_attempt) && (group = current_group) && (group <= 99))
You also need to change all
group <= 99
to
group < 99
Here is an improved version.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int group,
attempt,
group_total,
current_attempt,
current_group,
attempt_total;
FILE *ch7d = fopen("ch7d.dat", "r");
if (ch7d == NULL)
{
printf("Unable to open file.\n");
return 0;
}
fscanf(ch7d, "%d %d", &group, &attempt);
printf("Student Assessment Attempt Report\n");
while (group < 99)
{
group_total = 0;
current_group = group;
printf("\nGroup %d\n", current_group);
while (group == current_group && group < 99)
{
current_attempt = attempt;
attempt_total = 0;
/*stuck from here*/
while ((attempt == current_attempt) && (group == current_group) && (group < 99))
{
attempt_total += 1;
fscanf(ch7d, "\n%d %d", &group, &attempt);
//getch();
}
if (current_attempt == 2)
{
printf("Both ");
}
else if(current_attempt == 1)
{
printf("One ");
}
else
{
printf("None ");
}
printf("%d\n", attempt_total);
group_total += attempt_total;
}
printf("Group total:%d\n", group_total);
}
fclose(ch7d);
getch();
return 0;
}