I<3soccer 0 Newbie Poster

Hello,
I'm working on an inventory program in c and having a lot of trouble with it. I've compared my code to several examples in the book and it seems right but I'm having these problems:
1. When I try to list the tools, the program ignores the command and just reprints the menu
2. When I insert a new record after inserting the price it creates an infinite loop. I don't see anything that would cause this problem.
3. When updating a record the program does not save the price.

thanks in advance

#include <stdio.h>

struct Data{
	int toolnum;
	char toolname[15];
	int quantity;
	double price;
};

int enterchoice(void);
void List( FILE *readPtr);
void updateRecord( FILE *fPtr);
void newRecord (FILE *fPtr);
void deleteRecord (FILE *fPtr);

int main (void)
{
	FILE *hfPtr;
	int choice;

	if((hfPtr = fopen("hardware.dat","rb+"))==NULL){
		printf ("File could not be opened.\n");
	}
	else{
		while (( choice= enterchoice())!= 5){
			switch (choice){
				case 1:
					List(hfPtr);
					break;
				case 2 :
					updateRecord(hfPtr);
					break;
				case 3:
					newRecord (hfPtr);
					break;
				case 4:
					deleteRecord (hfPtr);
					break;
				default:
					printf("Incorrect choice\n");
					break;
				}
			}
			fclose (hfPtr);
		}
	return 0;
}

int enterchoice (void)
{
	int menuChoice;

	printf( "\nEnter Your Choice\n"
		"1 View list of tools\n"
		"2 Update a record\n"
		"3 Add a new record\n"
		"4 Delete a record\n"
		"5 End prgoram\n?");
	scanf ("%d", &menuChoice);
	return menuChoice;
}

void List(FILE *readPtr)
{
	FILE *writePtr;

	struct Data tools = { 0, "", 0, 0.0 };

	if ((writePtr = fopen( "tools.txt", "w" )) == NULL ) 
	{
		printf( "File could not be opened.\n" );
	} 
	else
	{ 
		rewind( readPtr );
		fprintf( writePtr, "%-6s%-16s%-11s%-10s\n","Record #", "Tool name", "Quantity", "Price" );

		/* copy all records from random-access file to text file */
		while ( !feof( readPtr ) ) 
		{
 			fread( &tools, sizeof( struct Data ), 1, readPtr );
 			/* write single record to text file */
 			if ( tools.toolnum != 0 ) 
			{
				fprintf(writePtr, "%-6d%-16s%-11d%10.2f\n", tools.toolnum, tools.toolname, tools.quantity, tools.price );
 			} 
		} 
		fclose( writePtr );
	} 
} 


void updateRecord(FILE *fPtr)
{
	int record;
	double price;

	struct Data tools= {0,"",0,0.0};

	printf("Enter record number to update (1-100):");
	scanf ("%d", &record);

	fseek( fPtr, (record-1)*sizeof(struct Data), SEEK_SET);

	fread( &tools, sizeof(struct Data),1,fPtr);

	if (tools.toolnum==0){
		printf("Record # %d has no information.\n", record);
	}
	else {
		printf ("Enter new tool name, quantity, and price:\n");
		scanf ("%s%d%f", &tools.toolname, &tools.quantity, &tools.price);
	
		printf("%-6d%-16s%-6d%10.2f\n", record, tools.toolname, tools.quantity, tools.price);

		fseek(fPtr, (record-1)*sizeof(struct Data), SEEK_SET);

		fwrite(&tools, sizeof(struct Data), 1, fPtr);
	}
}


void deleteRecord (FILE *fPtr)
{
	struct Data tools;
	struct Data blanktools = {0,"",0,0.0};
	int toolnumber;

	printf("Enter record number to delete (1-100):");
	scanf("%d", &toolnumber);

	fseek( fPtr, (toolnumber-1)*sizeof(struct Data), SEEK_SET);

	fread ( &tools, sizeof(struct Data),1,fPtr);

	if(tools.toolnum==0){
		printf("Record %d does not exist.\n", toolnumber);
	}
	else{
		fseek(fPtr, (toolnumber-1)*sizeof(struct Data), SEEK_SET);
		fwrite(&blanktools, sizeof(struct Data), 1, fPtr);
		printf("Record Deleted\n");
	}
}

void newRecord (FILE *fPtr)
{
	struct Data tools={0,"",0,0.0};
	int toolnumber;

	printf ("Enter new record number (1-100):");
	scanf ("%d", &toolnumber);

	fseek( fPtr, (toolnumber-1)*sizeof(struct Data),SEEK_SET);
	fread(&tools, sizeof(struct Data),1,fPtr);

	if (tools.toolnum !=0){
		printf("Record number already contains information.\n", tools.toolnum);
	}
	else{
		printf("Enter Tool Name, quantity, and price\n?");
		scanf("%s%d%1f", &tools.toolname, &tools.quantity, &tools.price);

		tools.toolnum = toolnumber;

		fseek(fPtr, (tools.toolnum - 1)* sizeof(struct Data), SEEK_SET);

		fwrite(&tools, sizeof(struct Data), 3,fPtr);
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.