GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Oct-2004, 16:07
Newworld Newworld is offline
New Member
 
Join Date: Sep 2004
Posts: 7
Newworld is on a distinguished road

C binary file program


I'm writing a program to store information about students using structures. We store the information in a binary file. I am getting some compiler errors using the unix machines at school. I can't see the errors that it is giving me. Any help would be greatly appreciated.
Here are the errors:

project2.c:17: two or more data types in declaration of `student'
project2.c: In function `delete':
project2.c:62: `rec' undeclared (first use in this function)
project2.c:62: (Each undeclared identifier is reported only once
project2.c:62: for each function it appears in.)
project2.c: In function `print':
project2.c:114: warning: assignment makes integer from pointer without a cast
project2.c:115: request for member `print' in something not a structure or union
project2.c:142:2: warning: no newline at end of file

CPP / C++ / C Code:
#include <stdio.h>

FILE *f;
/*Struct rec will be the student format*/
struct rec 
{	
	long id;
	char name[30];
	unsigned char age;
	unsigned char t1, t2, t3;
	unsigned char p1, p2, p3, p4, p5;
	unsigned int sex:1;
	unsigned int class:3;
}
/*Function for writing student information at beginning semester*/
void student()
{
	int number, i,j;
	printf("How many total students?\n",number);
	struct rec r[number];
	
	for( i=0; i<number; i++)
	{	
		printf("Student number %d, enter name\n",i);
		fgets(r[i].name,30,stdin);
		fflush(stdin);
		printf("Enter id\n");
		scanf("%d\n",&j);
		fflush(stdin);
		r[i].id=j;
		printf("Enter age\n");
		scanf("%d\n",&j);
		fflush(stdin);
		r[i].age=j;
		printf("Enter 0 for male, 1 for female\n");
		scanf("%d\n",&j);
		fflush(stdin);
		r[i].sex=j;
		printf("Enter 0 - Freshman, 1 - Sophmoore, 2 - Junior, 3 - Senior\n");
		scanf("%d\n",&j);
		fflush(stdin);
		r[i].class=j;
		fwrite(&r[i], sizeof(struct rec),1,f);
	}
	
	

}
/*Function to delete a student from the list*/
void delete()
{
	int ret, i,j,std;
	/*use rec zero as a blank record to overwrite student*/
	struct rec zero; 
	printf("How many students to delete?\n");
	scanf("%d\n",&i);
	fflush(stdin);
	for (j=0;j<i;j++)
	{	printf("Enter student number\n");
		scanf("%d\n",&std);
		fflush(stdin);
		ret= fseek(f,std*(sizeof(rec)),SEEK_SET);
		fwrite(&zero,sizeof(struct rec),1,f);
	}
		
}
/*Function to input scores for students*/
void score()
{
	int ret, i, j,std;
	struct rec test[50];
	printf("How many students?\n");
	scanf("%d\n",i);
	fflush(stdin);
	for(j=0;j<i;j++)
	{
		ret= fseek(f,0,SEEK_SET);
		fread(&test[i],sizeof(struct rec),1,f);
	}
	for(j=0;j<i;j++)
	{	
		printf("Enter student number\n");
		scanf("%d\n",&std);
		fflush(stdin);
		fread(&test,sizeof(struct rec),1,f);
		printf("Enter test scores, then project scores\n");
		scanf("%d %d %d %d %d %d %d %d",&test[i].t1,&test[i].t2,&test[i].t3,&test[i].p1,&test[i].p2,&test[i].p3,&test[i].p4,&test[i].p5);
		fflush(stdin);
	}
	for(j=0;j<i;j++)
	{	
		ret= fseek(f,0,SEEK_SET);
		fwrite(&test[i],sizeof(struct rec),1,f);
	}
}
/*Function used to print current class information*/
void print()
{
	int ret, std, i;
	char s;
	struct rec print[50];
	printf("%-11d %-32d %-7d %-5d %-5d %-13d %-22d\n","   ID","       Name","Class","Sex","Age"," T Scores","   P Scores");
	printf("---------  ------------------------------  -----  ---  ---  -----------  --------------------  \n");
	printf("Enter number of students\n");
	scanf("%d\n",&std);
	fflush(stdin);
	ret= fseek(f,0,SEEK_SET);
	for(i=0;i<std;i++)
	{
		fread(&print[i],sizeof(struct rec),1,f);
	}
	for(i=0;i<std;i++)
	{
		s = print[i].sex==0?"M":"F";
		printf("%-11d %-32s %-7d %-5d %5c %-4c %-4c %-4c %-4c %-4c %-4c %-4c %-4c\n",print[i].id,print[i].name[30],print[i].class,s,print[i].age,print[i].t1.print[i].t2,print[i].t3,print[i].p1,print[i].p2,print[i].p3,print[i].p4,print[i].p5);
	}
}
int main(void)
{
	int choice;
	f = fopen ("record","rwtb");
	printf("Choose option by number,\n1 - Enter student information\n2 - Enter test or program scores\n3 - Print class report\n4 - Delete Students\n");
	scanf("%d\n",&choice);
	fflush(stdin);
	switch(choice)
	{	case 1: student();
			break;
		case 2: score();
			break;
		case 3: print();
			break;
		case 4: delete();
			break;
	}
	fclose(f);
	while (1)
   		malloc (1 << 20);
  	return 0; // not reached
	


}
Last edited by dsmith : 10-Oct-2004 at 17:38. Reason: Please use [c] & [/c] for syntax highlighting
  #2  
Old 10-Oct-2004, 16:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Newworld
I'm writing a program to store information about students using structures. We store the information in a binary file. I am getting some compiler errors using the unix machines at school. I can't see the errors that it is giving me. Any help would be greatly appreciated.
Here are the errors:

project2.c:17: two or more data types in declaration of `student'
project2.c: In function `delete':
project2.c:62: `rec' undeclared (first use in this function)
project2.c:62: (Each undeclared identifier is reported only once
project2.c:62: for each function it appears in.)
project2.c: In function `print':
project2.c:114: warning: assignment makes integer from pointer without a cast
project2.c:115: request for member `print' in something not a structure or union
project2.c:142:2: warning: no newline at end of file

CPP / C++ / C Code:
#include <stdio.h>

FILE *f;
/*Struct rec will be the student format*/
struct rec 
{	
	long id;
	char name[30];
	unsigned char age;
	unsigned char t1, t2, t3;
	unsigned char p1, p2, p3, p4, p5;
	unsigned int sex:1;
	unsigned int class:3;
} /* <--- missing semicolon */
/*Function for writing student information at beginning semester*/
void student() 

Same answer as you got on the other board: missing semicolon at end of struct{} definition.

Regards,

Dave
  #3  
Old 10-Oct-2004, 17:49
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
In addition, your use of printf isn't correct. Printf is not an input function only an output function. You need to use a some kind of input function here. I would use fgets...

CPP / C++ / C Code:

	int number, i,j;
	printf("How many total students?\n",number);
	struct rec r[number];
	
  #4  
Old 10-Oct-2004, 18:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by dsmith
In addition, your use of printf isn't correct. Printf is not an input function only an output function. You need to use a some kind of input function here. I would use fgets...

CPP / C++ / C Code:

	int number, i,j;
	printf("How many total students?\n",number);
	struct rec r[number];
	

Yeah, there are lots of things that require some work. I thought if he could get past the compiler errors, most of the errors will be made visible as he starts to execute the program. In particular, the error message caused by the missing semicolon was typically uninformative, but after a while you get a feel of how these things happen.

Regards,

Dave
  #5  
Old 10-Oct-2004, 19:32
Newworld Newworld is offline
New Member
 
Join Date: Sep 2004
Posts: 7
Newworld is on a distinguished road
hmm, ok I've tried doing the printf debugging and it seems that I have some major problem when I'm trying to enter the name using

Code:
fgets(r[i].name,30,stdin);


when entering the info it does prints "Enter name" and then goes straight to Enter ID... it doesn't give the user a chance to enter a name

when I use printf to check name its just all empty spaces...


Thanks for the help!
  #6  
Old 11-Oct-2004, 20:43
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Newworld. I have always struggled with the scanf function. Is there a scanf function prior to this fgets call? Scanf does not clear the newline from the input buffer.

I would recommend changing everything to use fgets. Once you read a string, you can convert it to an integer using atoi.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 03:26
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:38.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.