• C

Trouble filling a structure from a file...receiving error message incompatible types in assignment

I am trying to fill the structure STUREC and am experiencing problems with my fscanf statement.  Upon compilation I receive the following message: proj6.c:83: incompatible types in assignment.  Can someone help me determine the source of my error?

My code is as follows:

//Name...
//...
//...
//...
//This program is designed to

#include <stdio.h>
#include <string.h>
#define STR_SIZE 25
#define ARR_SIZE 10
#define TRUE 1
#define FALSE 0

typedef struct
{
   char *month;
   int date;
   int year;
} DATE;

typedef struct
{
   char fName[STR_SIZE];
   char lName[STR_SIZE];
   int gradeLevel;
   char ssn[STR_SIZE];
   DATE birthDate;
}STUREC;

typedef enum {false, true} BOOLEAN;

BOOLEAN openFile(int argc, char *argv[], FILE **fpIn);
int fillArray(FILE *fpIn, STUREC arr[]);
int getMonth(char *temp);
void sortArray(STUREC arr[], int numEls);
void swap(STUREC *stu1, STUREC *stu2);
void printArray(STUREC arr[], int numEls);
void closeFile(FILE *fpIn);

// do not change this function
int main(int argc, char *argv[])
{
   int numEls;
   STUREC arr[ARR_SIZE];
   FILE *fpIn;

   if (openFile(argc, argv, &fpIn))
   {
      numEls = fillArray(fpIn, arr);
      printArray(arr, numEls);
      sortArray(arr, numEls);
      printArray(arr, numEls);
      closeFile(fpIn);
   }

   return 0;
}

BOOLEAN openFile(int argc, char *argv[], FILE **fpIn)
{
   BOOLEAN success=false;
   if (argc >= 2)
   {
      *fpIn = fopen(argv[1], "r");
      if (*fpIn != NULL)
         success = true;
      else
      {
         printf("\nERROR: Unable to open file for reading.\n");
      }
   }
   else
      printf("\nERROR: Not enough command line arguments.\n\n");
   return success;
}

int fillArray (FILE *fpIn, STUREC arr [])
{
   int i;
   STUREC stu;
   for (i=0; i < ARR_SIZE; i++)
   arr [i] = fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName,
                     &stu.gradeLevel, stu.ssn);
}
csunnypl2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
the problem is not fscanf
fscanf returns an integer representing the numbers of fields it has correctly parsed, in this case could be 4
You are trying to assign 4 to arr[i]
I guess you want to copy contents of 'stu' to arr[i], so correct code would be:

int fillArray (FILE *fpIn, STUREC arr [])
{
   int i;
   STUREC stu;
   for (i=0; i < ARR_SIZE; i++)
   fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName,  &stu.gradeLevel, stu.ssn);
   arr[i] = stu;
/* alternative to previous line:
   memcpy(&arr[i], &stu, sizeof(stu));
*/
}

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
baboo_Commented:
Your problem's here:

arr [i] = fscanf (fpIn, "%s%s%d%s", stu.fName, stu.lName, &stu.gradeLevel, stu.ssn);

fscanf returns an int equal to the number of items it's read.  This line tries to assign that int value to an element in your STUREC array, arr.  This is what you want to do, I think:

fscanf (fpIn, "%s%s%d%s", arr[i].fName, arr[i].lName, &(arr[i].gradeLevel), arr[i].ssn);

And remove STUREC stu from your fillArray method (you don't use it anymore).

baboo_
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.