fredyrockz 0 Report post Posted June 29, 2007 Assalamualaikum.. 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.0021 Hard disk 1 GB 8 730.00 1000.0022 Hard disk 4 GB 5 1440.00 1730.0033 Monitor 15†4 550.00 700.0067 Speaker 3 50.00 88.00 so..apa yg patut saya mulakan?...aduh..pening kepala saya.. Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted June 29, 2007 secara bottom-up programming, awak kena settle masalah kecik dulu. yang paling asas iaitu, macam mana nak tulis dan baca dari fail teks. lepas solve tu, baru la cari jalan utk update, delete record. berdasarkan soalan diberi, file teks tersebut sesuai dengan format comma-separated values(CSV) Quote Share this post Link to post Share on other sites
fredyrockz 0 Report post Posted June 29, 2007 boleh awak tunjukkan contoh code ? terima kasih..mungkin pastuh saya dapat ilham... Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted June 29, 2007 nak ilham ke? aku rasa tak susah ke nak taip 'c++ csv' kat google. 2nd result ko dapat ni http://cm.bell-labs.com/cm/cs/tpop/code.html Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted June 29, 2007 sila baca Peraturan Dan Cara Berforum Quote Share this post Link to post Share on other sites
fredyrockz 0 Report post Posted June 29, 2007 (edited) maksud awak kena paste dlm codebox ke?..sorry... /* 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..?.. Edited June 30, 2007 by fredyrockz Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted June 29, 2007 screenshot aku bagi tu aku tulis code, bukan codebox. tu pun takleh ikut ka? Quote Share this post Link to post Share on other sites
unexistance 3 Report post Posted June 29, 2007 1st thing 1st..paham tak ape kod tu buat line-by-line? haa..cuba explain algorithm code tu dulu.. Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted June 29, 2007 ~ so...boleh x awak tolong tengok apa yg salah..?.. 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. Quote Share this post Link to post Share on other sites
fredyrockz 0 Report post Posted June 30, 2007 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? boleh bagi contoh x?.. Quote Share this post Link to post Share on other sites
encik pot pet 0 Report post Posted June 30, 2007 kalau ko sendiri yg buat program kat atas tu, aku yakin takde masalah nak fix. Quote Share this post Link to post Share on other sites
civ3 9 Report post Posted June 30, 2007 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? 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. Quote Share this post Link to post Share on other sites
fredyrockz 0 Report post Posted June 30, 2007 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? 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.. Alhamdulillahhh..sekarang boleh fix dah..thanks a lot lah kepada otai2 disini ok..civ3..ko nih aktif ke kat cboard tuh?.. Quote Share this post Link to post Share on other sites