Jump to content

Function In C Not Working Right, Need Help


Ghost2520
 Share

Recommended Posts

So i'm workin on this project right now... due tomorrow.... not very far. Basically, I've got an input file that has schedule information in the following format.

 

c /*this is the course prefix */

121 /* course number */

4 /*number of credits */

8 /*number of seats */

 

The input file has 11 of these groups. I need to make a function that reads one in, and does error checking to make sure its to the proper format. If it is, it the function returns a 1 for true. If its not, a 0 is returned. The actual values of each variable are sent back via pointers. I haven't implemented any errorchecking yet, but for some reason I can't get the pointers working for the string. 121, 4 and 8 are sent back properly but the c gets returned as a bunch of garble.

 

 

I've actually decided it would probably be better to define my own types using structures... so I've got

 


typedef struct{
	  char course_prefix[5];
	  int course_number;
	 int credits;
	   int seats;
}course_t;

int get_course_record (FILE *infile, course_t *current_course); /* prototype */

result = get_course_record (sched_in, &current_course); /* function call */


/*Function definition: */

int
get_course_record (FILE *infile, course_t *current_course)
{
char scan_prefix[strSIZE];
int scan_course_number;
int scan_credits;
int scan_seats;

fscanf(infile, "%c", scan_prefix);
fscanf(infile, "%d", &scan_course_number);
fscanf(infile, "%d", &scan_credits);
fscanf(infile, "%d", &scan_seats);

strcopy((*current_course).course_prefix, scan_prefix); /* I'm not sure if this is correct syntax here */
(*current_course).course_number = scan_course_number;
(*current_course).credits = scan_credits;
(*current_course).seats = scan_seats;

return 1;
}

Share this post


Link to post
Share on other sites

I've actually decided it would probably be better to define my own types using structures... so I've got

 


typedef struct{
	  char course_prefix[5];
	  int course_number;
	 int credits;
	   int seats;
}course_t;

int get_course_record (FILE *infile, course_t *current_course); /* prototype */

result = get_course_record (sched_in, &current_course); /* function call */


/*Function definition: */

int
get_course_record (FILE *infile, course_t *current_course)
{
char scan_prefix[strSIZE];
int scan_course_number;
int scan_credits;
int scan_seats;

fscanf(infile, "%c", scan_prefix);
fscanf(infile, "%d", &scan_course_number);
fscanf(infile, "%d", &scan_credits);
fscanf(infile, "%d", &scan_seats);

strcopy((*current_course).course_prefix, scan_prefix); /* I'm not sure if this is correct syntax here */
(*current_course).course_number = scan_course_number;
(*current_course).credits = scan_credits;
(*current_course).seats = scan_seats;

return 1;
}

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...