0

I am reading a list of grades from a txt file into an array. It worked fine when reading user input, but I'm having trouble reading each line when scanning from file. The number of students is variable. The number of grades per student is variable. I have no trouble reading the number of students and number of assignments, but when reading from file I'm having trouble pulling the int (grade) from each line for each student. The input may be like a or b (or any larger number of students/assignments):

txt-example1 (the comments including and after // are my own and not in txt file)

2  //number of students
3  //the number of grades per student (will match the number of grade rows below)
theo  alvin //the number of names will match the number of students
75 60
89 90
79 95

txt-example2

3
4
theo alvin simon 
78 85 90
85 96 76 
77 99 100
88 55 92

I can put the names into 1 dimension of a 2d array (I'll use the second dimension later to print - no problems with that part). I want to get the grades into a 2d array. Here is what I have

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

int numStus;
int numGrades;

int main()
{
   FILE* inputFile;
   char stuNames[numStus][10];
   int grades[numGrades][numStus];

   inputFile = fopen("testData.txt","r"); //assume inputFile has data from Ex 1 or 2 above

   fscanf(inputFile,"%d",&numStus);

   fscanf(inputFile,"%d",&numGrades);

   int i;
   int j;

   for (i=0; i<numStus; i++)
   {
      fscanf(inputFile,"%s",&stuNames[i]);
   }

   //here is where I'm having trouble

      for(i=0;i<numGrades;i++)
   {
      for(j=0;j<numStus; j++)
      {
        //I want to use fscanf, but don't know how to account for carriage returns to iterate into next part of array
      }   
   }

}  

What worked when getting from user input:

int i;
int j;
int k;

for (i=0; i<numGrades; i++)
{
   for (j=0; j<numStus; j++)
   {
      printf("Enter grade for Assignemnt %d for ",i)

      for(k=0;k<10;k++)
      {
         printf("%c",stuNames[j][k]);
      }
      scanf("%d",&grades[i][j]);
   }
}

The part immediately above worked well for user input grades. When getting the same input from a file I'm not sure how to get the grades into the proper dimensions. Any advice on how to account for the newline/CR to increment the array would be very much appreciated. Thanks.

3 Answers 3

1

The scanf can be used in the nested loops as usually for reading a single value, since the carrige return is skipped as a space, for example: fscanf(inputFile,"%d",&grades[i][j]);

However, the arrays stuNames and grades must be initialized only after reading numStus and numGrades, for example:

...
fscanf(inputFile,"%d",&numStus);
char stuNames[numStus][10];

fscanf(inputFile,"%d",&numGrades);
int grades[numGrades][numStus];
...

That trick is not allowed in ANSI C. In that case dynamic memory allocation should be used.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! What I didn't realize was what you mentioned above - that a carriage return is treated as a space. I rearranged the order as the other comments suggested, then just added the scanf in and disregarded the CR since they were already taken into a account (iterating at j<numStus). for(i=0; i<numGrades;i++)
0

You have trouble much earlier than you think. You can't use uninitialized variables when you are declaring your arrays. In your working example you don't show how you allocated your arrays. Were they fixed size? Whatever you did there would probably work in your new program.

Comments

0

You first need to read the amount of students and amount of grades.

A uninitialized variable like int foo; has a "random" value. You need to initialize the variable like int foo = 0;.

Knowing this lets analyze your code step by step.

int numStus; //numStus gets random value
int numGrades; //numGrades gets random value

int main()
{
   FILE* inputFile;
   char stuNames[numStus][10]; //random amount gets generated
   int grades[numGrades][numStus]; //variable gets random value
   fscanf(inputFile,"%d",&numStus); //numStus gets actual value
   fscanf(inputFile,"%d",&numGrades); //numGrades gets actual value

C is a language which handles things in order. This means that the array keeps the size is has been given.

The code can be fixed by switching around the statements like this:

int numStus; //numStus gets random value
int numGrades; //numGrades gets random value

int main()
{
   FILE* inputFile;
   fscanf(inputFile,"%d",&numStus); //numStus gets actual value
   fscanf(inputFile,"%d",&numGrades); //numGrades gets actual value
   char stuNames[numStus][10]; //array of wanted size gets created
   int grades[numGrades][numStus]; //random amount gets

I hope this helps, may you have any questions ask them

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.