I'm supposed to create a program that reads in the name of a file, then takes the names of students, which are listed (last, first, m) and concatnate the first with the last (firstlast) and add .dat at the end for each student to open a file with and unknown number of grade for that student, and avg the grades together.
Then, I must list the class average, the maximum score, and the minimum score. I only have so much right now and I honeslty don't know if I'm even on the right track.
HELP???

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
    FILE *inputfile, *infile;

    double mark;
    int grade;
    int avg = 0.0;
    int i = 0;
    char student[30];
    char filename[30];
    char *first, *last, *middle;

    printf ("Enter Name of Data File: \n");
    scanf ("%s", filename);

    inputfile = fopen( filename , "r");

    while ((fscanf (inputfile, "%s", student) != EOF))
    {
        for (i = 0;i>=0;i++)
        {
            fscanf(inputfile, "%s %s %c", last, first, middle);
            infile = fopen( strcat(first,last).dat, "r");

        for (grade = 0; grade != 0; grade++)
        {
            fscanf (infile, "%d", &mark);
            mark = mark + mark;
            avg = mark/grade;
        }
        }
    }
    return 0;
}

Recommended Answers

All 3 Replies

HELP???

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()
{
	FILE *inputfile, *infile;
	
	double mark;
	int grade;
???	int avg = 0.0;  
	int i = 0;
	char student[30];
	char filename[30];
	char *first, *last, *middle;

	printf ("Enter Name of Data File: \n");
	scanf ("%s", filename);

	inputfile = fopen( filename , "r");

Not "student". You will create "student" string. You need to take in %s %s %s for last
name, first name and middle initial. Also, last ) should go before "!".

???	while ((fscanf (inputfile, "%s", student) != EOF))<-Two } before !, one } here
	{
		for (i = 0;i>=0;i++)
		{
??? Move upward ^^			fscanf(inputfile, "%s %s %c", last, first, middle);

?? create student string, slightly better, imo.
	infile = fopen( strcat(first,last).dat, "r");

??? grade=0; grade != 0 ??? loop will never loop! Use another while loop, not 
a for loop if that helps your thinking. I would use a while loop.

???		for (grade = 0; grade != 0; grade++)
		{
			fscanf (infile, "%d", &mark);
??? You can't keep a sum building, when you're replacing the value with fscanf(),
each time through the loop. Use a sum variable: sum += mark;

???			mark = mark + mark;
			avg = mark/grade;
		}
		}
	}
	return 0;
}

If you type your own code tags, the slash on the closing tag is a / not a \.

Good start, but a few rough edges.

That helped a lot. I just can't seem to get it to print to two decimal places now, which is what is required.

#include <stdio.h>
#include <math.h>
#include <string.h>

int main()

{

	FILE *inputfile, *infile;
	
	int sum = 0.00, sum2 = 0.00;
	double mark;
	double avg = 0.00, cavg = 0.00;

	int grade = 0, count = 0;
	int min = 0, max = 0;

	char filename[30], dat[5] = ".dat";
	char first[30], first1[30], last[30], middle[5];

	printf ("Enter Name of Data File: \n");
	scanf ("%s", filename);

	if ((inputfile = fopen( filename, "r")) == NULL)
	{
		printf ("Cannot open file!\n");
		return 1;
	}

	
	printf ("Student\t\tAverage\n");

	while ((fscanf (inputfile, "%s", last)) != EOF)
	{
		fscanf(inputfile, "%s %s", first, middle);
		strcpy(first1,first);
		strcat(first,last);
		strcat(first,dat);

		infile = fopen( first, "r");

		while ((fscanf (infile, "%i", &mark)) != EOF)
		
		{
			fscanf (infile, "%i", &mark);
			sum += mark;
			grade++;
			avg = sum/grade;
			sum2 += mark;
			count++;

		}
		sum = 0;
		grade = 0;

		printf ("%s %s %s\tl%d\n", first1, middle, last, avg);
	}
		cavg = sum2/count;
		printf ("Class average:\t%f\n", cavg);
		printf ("Max Score:\t%i\n", max);
		printf ("Min Score:\t%i", min);
		fclose (inputfile);

	return 0;
}

Also, I've tried a few things to get the min terma and max terms. I figured something like this, placed inside the nested while loop, after I have initialized max at 0.

for (mark > max)
{
     max = mark;
}

Or am I going to need a different while loop entirely?

I figured it out. Thanks again Adak!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.