Jump to content
Sign in to follow this  
fredyrockz

Inventory Records

Recommended Posts

Assalamualaikum.. :rolleyes:

Saya newbie disini..

saya nak mintak bantuan otai2 kat sini tentang masalah saya nih..

soklan nye seperti di bawah :-

You are the owner of a computer shop and need to keep an inventory that can tell you what components you have, how many you have, the cost for each component and the selling price.

Write a program that creates a file named “Computer.txt†to 100 empty records, let you input the data concerning each tool, enables you to list all the components, lets you delete record and let you update any information in the file. The component identification number should be the record number. Use the following information to start your file:

Record Component Quantity Cost (RM) Selling Price (RM)

1 Mouse 7 15.00 23.00

21 Hard disk 1 GB 8 730.00 1000.00

22 Hard disk 4 GB 5 1440.00 1730.00

33 Monitor 15†4 550.00 700.00

67 Speaker 3 50.00 88.00

so..apa yg patut saya mulakan?...aduh..pening kepala saya.. :mellow::unsure:

Share this post


Link to post
Share on other sites

maksud awak kena paste dlm codebox ke?..sorry... :blush:

/* hardwareware.dat */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

/* hardwareData structure definition */
struct hardwareData {
int recordNum;
char toolname[30];
int quantity;
float cost;

}; /* end structure hardwareData */

/* prototypes */
int enterChoice( void );
void textFile( FILE *readPtr );
void updateRecord( FILE *fPtr );
void newRecord( FILE *fPtr );
void deleteRecord( FILE *fPtr );



int main()
{
FILE *hfPtr; /* hardware.dat file pointer */
int choice; /* user choice */

/* fopen opens file; exits if file cannot be opened */
if ((hfPtr = fopen( "hardware.dat", "rb+" )) == NULL ) {
printf( "File could not be opened.\n" );
} /* end if */
else {

/* enable user to specify action */
while (( choice = enterChoice()) != 5 ) {

switch ( choice ) {
/* create text file from record */
case 1: 
textFile( hfPtr );
break;

/* update record */
case 2: 
updateRecord( hfPtr );
break;

/* create record */
case 3:
newRecord ( hfPtr );
break;

/* delete existing record */
case 4:
deleteRecord ( hfPtr );
break;

/* display message if user does not enter valid choice */
default:
printf( "Incorrect choice\n" );
break;

} /* end switch */

} /* end while */

fclose( hfPtr ); /* fclose closes the file */

} /* end else */

return 0; /* indicate successful termination */

} /* end main */

/* create formated text for printing */
void textFile( FILE *readPtr )
{
FILE *writePtr = NULL; /* hardware.txt file pointer */

/* create hardwareData with default information */
struct hardwareData hardware = { 0, "", 0,0.0 };

/* fopen opens file; exits if file cannot be opened */
if ((writePtr = fopen( "hardware.txt", "r+" )) == NULL ) {
printf( "File could not be opened.\n" );
} /* end if */
else{ 
rewind( readPtr ); /* sets pointer to begining of file */
fprintf( writePtr, "%-6s%-10s%-8s%-8s\n",
"Record#", "Tool name", "Quantity", "Cost" );

/* copy all records from random-access file to text file */
while ( !feof( readPtr ) ) {
fread( &hardware, sizeof( struct hardwareData ),1, readPtr );

/* write single record to text file */
if ( hardware.recordNum != 0 ) {
fprintf( writePtr, "%d%s%d%f",
hardware.recordNum, hardware.toolname, 
hardware.quantity, hardware.cost );
} /* end if */

} /* end while */


fclose( writePtr ); /* fclose closes file */
} /* end else */

} /* end function textrFile */

/* update record */
void updateRecord ( FILE *fPtr )
{
int record; /* record number */

/* create record data with no information */
struct hardwareData hardware = { 0, "", 0,0.0 };

/* obtain number of account to update */
printf( "Enter record to update (0-100): ");
scanf( "%d", &record );

/* move file pointer to correct record */
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );

/* read record from file */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );

/* display error if record does not exist */
if ( hardware.recordNum == 0) {
printf( "Record #%d has no information.\n", record );
} /* end if */
else { /* update record */
printf( "Enter new tool name, quantity,cost \n?" );
scanf( "%s %d %f", hardware.toolname, &hardware.quantity, &hardware.cost );

fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );
/* write updated record over old record */
fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
} /* end else */

} /* end function update record */

/* delete an existing record */
void deleteRecord( FILE *fPtr )
{

struct hardwareData hardware; /* stores record read form file */
struct hardwareData blankRecord = { 0, "", 0,0.0 }; /* blank record */

int recordNum; 

/* obtain number of record to delete */
printf( "Enter record number to delete (0-100): " );
scanf( "%d", &recordNum );

/* move file pointer to correct file */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );

/* read record from file */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );

/* display error if record does not exist */
if ( hardware.recordNum==0 ) {
printf ( "Record %d does not exist.\n", recordNum );
} /* end if */
else { /* delete record */

/* move file pointer to correct record */
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ),
SEEK_SET );

/* replace existing record with blank record */
fwrite( &blankRecord,
sizeof( struct hardwareData ), 1, fPtr );
} /* end else */

} /* end function delete record */

/* create and insert record */
void newRecord( FILE *fPtr )
{
/* create recordData with default information */
struct hardwareData hardware = { 0, "", 0,0.0 };

int piece; 

/* obtain record number to create */
printf( "Enter record number to create (1-100) : " );
scanf( "%d", &piece);

/* move file pointer to correct record */
fseek( fPtr, piece * sizeof( struct hardwareData ),
SEEK_SET );

/* read record */
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );

/* display error if record already exists */
if ( !hardware.recordNum ) { /* create record */
hardware.recordNum = piece;
printf( "Enter tool name, quantity, cost\n?" );
scanf( "%s %d %f", hardware.toolname, &hardware.quantity, &hardware.cost );

/* move file pointer to correct record */
fseek(fPtr, hardware.recordNum * sizeof( struct hardwareData), SEEK_SET);

/* insert record into file */
fwrite( &hardware, sizeof( struct hardwareData), 1, fPtr);
} /* end if */
else { 
/* user enters toolname, quantity and cost */
printf( "Record already exists.\n" );
} /* end else */

} /* end function newRecord */



/* enable user to input menu choice */
int enterChoice( void )
{

int menuChoice; /* variable to stoer users choice */

/* display available options */
printf( "\nEnter your choice\n"
"1 - store a formatted text file of records called\n"
" \"hardware.txt\"for printing\n"
"2 - update a record\n"
"3 - add a new record\n"
"4 - delete a record\n"
"5 - end program\n? " );
scanf( "%d", & menuChoice ); /* recieve choice from user */

return menuChoice;

} /* end function enterChoice */

~ so...boleh x awak tolong tengok apa yg salah..?.. :rolleyes:

Edited by fredyrockz

Share this post


Link to post
Share on other sites

~ so...boleh x awak tolong tengok apa yg salah..?.. :rolleyes:

aku dah test tengok. problem yg aku jumpa masa nak convert file binary ke teks. satu lagi, kalau file tu tak wujud, sepatutnya program tu kena create la.

Share this post


Link to post
Share on other sites

aku dah test tengok. problem yg aku jumpa masa nak convert file binary ke teks. satu lagi, kalau file tu tak wujud, sepatutnya program tu kena create la.

so..kat bahagian mana nak kena perbetul nih? :wacko:

boleh bagi contoh x?.. :ph34r::blush:

Share this post


Link to post
Share on other sites

hai fredyrockz. soalan assignment ini, kolej mana punya awak postkan? kolej UNIT** punya ke? dan jangan ingat saya tak tahu mana awak ambil coding tersebut. coding itu diambil dari forum Cboard right? B)

kalau nak ambil coding orang lain pun, pastikan awak tahu bagaimana coding tersebut berfungsi. dan jika awak sendiri yang buat coding tersebut, awak akan tahu bahagian mana yang nak diperbetulkan.

Share this post


Link to post
Share on other sites

hai fredyrockz. soalan assignment ini, kolej mana punya awak postkan? kolej UNIT** punya ke? dan jangan ingat saya tak tahu mana awak ambil coding tersebut. coding itu diambil dari forum Cboard right? B)

kalau nak ambil coding orang lain pun, pastikan awak tahu bagaimana coding tersebut berfungsi. dan jika awak sendiri yang buat coding tersebut, awak akan tahu bahagian mana yang nak diperbetulkan.

hmm..memang saya amik dari forum cboard..of course..dan saya fix dengan kawan saya program tuh.. :blush:

Alhamdulillahhh..sekarang boleh fix dah..

thanks a lot lah kepada otai2 disini ok..

civ3..ko nih aktif ke kat cboard tuh?.. :blink:

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...