Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ketema@ketema.n

macrumors newbie
Original poster
Mar 26, 2005
8
0
I need help with structures. So far my learning experience with C and Xcode has been great, but I am frustrated over structures and typedefs I have copied examples straight out the K&R book, they work, yet when I try my own they don't. The errors for the below code:

19: error: parse error before "readRecords"
19: warning: data definition has no type or storage class
49: warning: initialization makes pointer from integer without a cast
85: error: parse error before "readRecords"
94: error: `gradeRecord' undeclared (first use in this function)
94: error: parse error before "tempRecord"
95: error: `tempRecord' undeclared (first use in this function)


Code:
//Library inclusions
#include <stdio.h> 
#include <ctype.h>
#include <string.h>

//Constants
#define FLUSH while(getchar() != '\n'); 
#define MAXCHARS 80

//function headers
void prnMenu(int *menuChoice); //prints the menu
p2GradeRecord readRecords(FILE *); //This wrong to the compiler
//I thought when you typedef something it acts just like any other type so
//a function header is simply type identifier(argList);

int main (int argc, const char * argv[]) {
	//variables
	FILE *inputFile;
	
	typedef struct gradeRecord{
		char *firstName;
		char *lastName;
		int *scores;
	}GradeRecord;

	typedef struct gradeRecord *p2GradeRecord;
	
	inputFile = fopen("input.txt", "r"); //open the input file
	
	if (inputFile != NULL){
		p2GradeRecord gradeList = readRecords(inputFile);
	}
	
	do{
	prnMenu(menuChoicePtr);
		switch ( menuChoice ){
			case 1 :
				
				break;
			case 2 :
				if ( 1){
				
				}
				else{
					printf("No grades entered!\n");
				}
				break;
			case 3 :
				if(1){
					
				}
				else{
					printf("You must enter scores and compute statistics first!\n");
				}
				break;
			case 4 :
			
			case 5 :
				printf("Have a nice day!\n");
				return 0;
		}
	}while(menuChoice != 5);
	
	return 0;
}

p2GradeRecord readRecords(FILE *file){
	char fname[MAXCHARS];
	char lname[MAXCHARS];
	int score1 = 0;
	int score2 = 0;
	int score3 = 0;
	int score4 = 0;
	
	while((fscanf(file, "%s%s%d%d%d%d", fname, lname, &score1, &score2, &score3, &score4)) != EOF){
		gradeRecord tempRecord = (gradeRecord) malloc(sizeof(gradeRecord));		
		if((tempRecord->firstName = (char *) malloc(strlen(fname) * sizeof(char))) == NULL){
			printf("\nInsufficient Memory\n");
			exit(1);
		}
		strcpy(tempRecord->firstName, fname);
		if((tempRecord->lastName = (char *) malloc(strlen(fname) * sizeof(char))) == NULL){
			printf("\nInsufficient Memory\n");
			exit(1);
		}
		strcpy(tempRecord->lastName, lname);
			if((tempRecord->scores = (int *) malloc(4 * sizeof(int))) == NULL){
				printf("\nInsufficient Memory\n");
				exit(1);
			}
			tempRecord->scores[0] = score1;
			tempRecord->scores[1] = score2;
			tempRecord->scores[2] = score3;
			tempRecord->scores[3] = score4;
	}
	return &tempRecord;
}
 

ketema@ketema.n

macrumors newbie
Original poster
Mar 26, 2005
8
0
I fixed my code a little bit. The book doesn't make it clear that the structs have to be defined outside of main. I'm still getting a wierd error though...Perhaps some help on this one?


Code:
//Library inclusions
#include <stdio.h> 
#include <ctype.h>
#include <string.h>

//Constants
#define FLUSH while(getchar() != '\n'); 
#define MAXCHARS 80

struct gradeRecord{
	char *firstName;
	char *lastName;
	int *scores;
};

//function headers
void prnMenu(int *menuChoice); //prints the menu
struct gradeRecord readRecords(FILE *);

int main (int argc, const char * argv[]) {
	//variables
	FILE *inputFile;
	
	inputFile = fopen("input.txt", "r"); //open the input file
	
	if (inputFile != NULL){
		struct gradeRecord gradeList = readRecords(inputFile);
	}
	
	return 0;
}

struct gradeRecord readRecords(FILE *file){
	char fname[MAXCHARS];
	char lname[MAXCHARS];
	int score1 = 0;
	int score2 = 0;
	int score3 = 0;
	int score4 = 0;
	
	while((fscanf(file, "%s%s%d%d%d%d", fname, lname, &score1, &score2, &score3, &score4)) != EOF){
		struct gradeRecord tempRecord;
		
		tempRecord.firstName = (char *) malloc(strlen(fname) * sizeof(char));
		tempRecord.lastName =  (char *) malloc(strlen(lname) * sizeof(char));
		tempRecord.scores =  (int *) malloc(4 * sizeof(int));
		
		strcpy(tempRecord.firstName, fname);
		strcpy(tempRecord.lastName, lname);
		tempRecord.scores[0] = score1;
		tempRecord.scores[1] = score2;
		tempRecord.scores[2] = score3;
		tempRecord.scores[3] = score4;
	}
	return tempRecord;  //Errors here saying tempRecord is undeclared.
}
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
You have declared the variable tempRecord inside the while loop. Therefore it's scope is restricted to the while loop. As soon as you drop out of the loop the variable is no longer in scope and cannot be referred to. You need to put the variable definition at the top of the function with all the other variables.

Note that due to the way you have done things you will only return a single record, even though you are looping through a while loop as each time through you are wiping over the variable.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.