28. april 2002 - 03:07Der er
24 kommentarer og 2 løsninger
200 points for at finde fejlene!
Hej allesammen, Jeg poster her mit program, hvor jeg har problemet med mine funktioner og nok ogsaa function calls. Jeg har skrevet kommentarer ved functionerne hvad de skal kunne. Jeg tror at det har noget med structen at goere. Forhaabentlig er der en der kan kopiere programmet og kompile det og evt se nogle af fejlene. Ellers vil jeg kopiere fejlene fra kompileren. Debug muligheden skal printe output ved hver function.
Lad mig vide hvis jeg mangler at give noget information. Tak paa forhaand! Jeg er altid villig til at give flere points for et godt svar!! venlig hilsen Henrik
struct record { char name[32]; char address[80]; char telno[15]; float gpa; record *next; };
//Prototypes char enterChoice (); int addRecord (record*&, char[],char[],char[], float); void findRecord(record*, char name[]); void printAll(record*); int deleteRecord(record *&, char name[]); int readFile(record *&, char filename[]); int writeFile(record *, char filename[]); int isRecordInList (record *, char[]);
// global variable bool debugmode=false;
int main (int argc, char* argv[]) { record *start; start = NULL; int numstudents;
enum choices {a,f,p,d,r,w}; //choices the user has to either int loop = 0; //add, find, delete or print char enterChoice; //********************Input *************************************
if(debugmode) cout << "If debugmode is true, print debug-information!"<< endl;
cout << " MENU" << endl << " ****" << endl<<endl
<< "a - Add new student to the database\n" << "f - Print information about a student in the database\n" << "p - Print all information from the database\n" << "d - Delete an existing student from the database\n" << "r - Read your file\n" << "w - Write your file\n" << endl;
cout << "Enter your choice"<< endl; cin >> enterChoice;
//*********************Process ********************************** // The while loop will prompt the user for information as the // user type either a, f, p and d on the menu. while (loop <1) { switch (enterChoice){ case 'a': cout << "Please enter the students name: "<<endl; cin.ignore (100, '\n'); cin >> start[0].name,25; cout << "What is the address: "<< endl; cin.ignore (100, '\n'); cin >> start[0].address; cout <<"What is the telephone number: "<< endl; cin.ignore (100, '\n'); cin >> start[0].telno,15; cout << "What is the GPA: " << endl; cin.ignore (100, '\n'); cin >> start[0].gpa; //function call for function addRecord addRecord (start, start[0].name, start[0].address, start[0].telno, start[0].gpa); ++loop; break;
case 'f': cout<< "Please enter the students name: "<< endl; cin.ignore (100, '\n'); cin.getline (start[0].name, 25); //function call for function findRecord findRecord(start, start[0].name); ++loop; break;
case 'p': cout <<"Please print all information: "<<endl; start[0].gpa = 4.0; //function call for function printAll printAll(start); ++loop; break;
case 'd': cout<< "Please enter the students name: "<< endl; cin.ignore (100, '\n'); cin.getline (start[0].name, 25); //function call for function deleteRecord deleteRecord (start,start[0].name); ++loop; break;
} } cout << "Please type a number of students: " << flush; cin >> numstudents; cout << "You typed: " << numstudents << " students" << endl;
cout << "Writes " << numstudents << " students til file" << endl; writeFile(start, "filename.cpp");
readFile(start, " filename.cpp"); cout << "Read " << numstudents << " students from file" << endl;
return 0; }
//functions for the database
// addRecord prompts the user for name, // gpa, address and telno adds the student to the database. // addRecord must add the record to the end of the list // and will NOT create a record if it already exists with // the same name. int addRecord (record*&, char[],char[],char[], float) {
// isRecordInList checks for duplicates if (!isRecordInList(start, start[0].name)) { // Name couldn't be found in the list, add new record char ptr; char newrecord; ptr = newrecord; while (ptr) { ptr = ptr->next; } // TODO: // ptr points now at the last record in the list. // allocate a new record, and link them to the list.
} cout<< "A student has been added"<< endl; return 0; } // findstudent prompt the user for a students's name and and // prints information about a student from the database void findRecord(record*, char name[]) { cout<<"Information about a student has been found"; cout<< endl; }
// printAll will print the entire address book on the screen. void printAll(record*) { printf (record*); cout<<"The following information has been printed"<<endl; }
// deleteStudent prompts the user for a student's name and // will delete only the first matching record based on the name // in case they are duplicates (by accident) int deleteRecord(record *&, char name[]) { record *last, *current; last = NULL; current = start; while (current != NULL) { if (strncpy(current->navn, post->navn, sizeof(current->navn)-1) == 0) { if (last == NULL) start = current->next; else last->next = current->Next; return true; }; last = current; current = current->next; } return false; }
//function readfile reads data from file, copies to buffer, displays int readFile(record Stud*&, char filename[]) { //opens the file for input ifstream ifs(cFileName); nStudents=0; while(ifs.good()) { ifs >> StudArray[nStudents].name; ifs >> StudArray[nStudents].address; ifs >> StudArray[nStudents].telno; ifs >> StudArray[nStudents].gpa; ++nStudents; ifs.close (),//close the file } }
// Function writefile prints to file int writeFile(record Stud*, char filename[]) { int nStudents; //opens the file for output ofstream ofs(cFileName); for(int i=0; i<nStudents; ++i) { ofs << Stud[i].name << endl; ofs << Stud[i].address << endl; ofs << Stud[i].telno << endl; if(i == nStudents-1) // Do not enter a new line if it is the last line. ofs << Stud[i].gpa; else ofs << Stud[i].gpa << endl; ofs.close();//close the file } return 0; } // this function will check for duplicates int isRecordInList (record *, char[]) { }
Der er mange ting, som du har kludrer lidt rundt i, men lad os tage en funktion ad gangen:
Du mangler variable navne på mange af dine funktioner. Jeg har tilføjet liste her, og det navn du leder efter for Key.
int isRecordInList (record *liste, char* Key) { record* ptr=liste; // ptr peger nu på starten af listen
while (ptr) // sålænge ptr peger på en record { if (strcpy(ptr->name,Key)==0) // sammenlign name, for hver record { // record found return 1; } ptr->next; // Hop videre til næste record. } return 0; // Key blev ikke fundet i listen. }
Du har glemt at allokere en ny record. Dette er gjort her.
int addRecord (record* liste, char* NewName,char* NewAddress,char* NewTelNo, float Newgpa) { // isRecordInList checks for duplicates if (!isRecordInList(liste, NewName)) { // Name couldn't be found in the list, add new record
// Traverse to the end of the list. record* ptr=liste; while (ptr) ptr = ptr->next;
// allocate a new record record* newrecord; newrecord = (record*)malloc(sizeof(record));
if (newrecord) { // Put data into record strcpy(newrecord->address,NewAddress); strcpy(newrecord->name,NewName); strcpy(newrecord->telno,NewTelNo); newrecord->gpa = Newgpa; newrecord->next = NULL;
// Add the record to the list. ptr->next = newrecord; }else { // Could allocate memory for a new record. } } }
while (ptr) { if (strcpy(ptr->name,Name)==0) { // record found cout<<"Information about a student has been found"; .... udskriv alle data. ptr peger på den record der søges efter. cout<< endl; return; } ptr->next; } }
Jeg har ikke prøvet at fejlfinde i dit, har istedet for anvendt noget af dit og skrevet selv. Det er godt nok ikke det du har spurgt efter :-) men det virker. Der kunne godt være lidt mere fejl tjeck hist og pist, men det er optil dig.
// TestMe3.cpp : Defines the entry point for the console application. //
<< "a - Add new student to the databasen" << endl << "f - Print information about a student in the databasen" << endl << "p - Print all information from the databasen" << endl << "d - Delete an existing student from the databasen" << endl << "r - Read your filen" << endl << "w - Write your filen" << endl << "q - quit" << endl << endl;
cout << "Enter your choice: "; cin >> enterChoice;
end = processInput( pRecords, enterChoice ); }
deleteAllRecords(pRecords); return 0; }
bool processInput( record *&pRecords, char input[] ) { switch( input[0] ) { case 'q': { return true; }
Hej med jer! Jeg skal love for at i har vaeret flittige - det saetter jeg stor pris paa. chries, det virker hvad du har lavet, men desvaerre er det kraevet at jeg bruger foelgende: //prototyper char enterChoice (); int addRecord (record*&, char[],char[],char[], float); void findRecord(record*, char name[]); void printAll(record*); int deleteRecord(record *&, char name[]); int readFile(record *&, char filename[]); int writeFile(record *, char filename[]); int isRecordInList (record *, char[]);
//global variable debugmode
//local variable within main: record *start; start=NULL;
name field key in the database.
debug option should print output within each function.
Det er altsammen hvad jeg proevede paa i mit program, men altsaa uden at goere det rigtigt. Jeg ved ikke om i har mod paa at kigge lidt paa det? mange tak!
Hej cries, Tak for endnu engang at tage dig tid til nybegyndere som jeg. Ja, void findRecord(record*, char name[]) skal finde recorden og printe den til skaermen - det er desvaerre det eneste jeg kan informere om.
nu har modificeret det til at, det passer med funktionerne :-) hvis du hellere vil have jeg retter i dit kode, vil jeg gerne have du retter op på det, så det kan compilreres.
// TestMe4.cpp : Defines the entry point for the console application. // /* int addRecord (record*&, char[],char[],char[], float); void findRecord(record*, char name[]); */
<< "a - Add new student to the databasen" << endl << "f - Print information about a student in the databasen" << endl << "p - Print all information from the databasen" << endl << "d - Delete an existing student from the databasen" << endl << "r - Read your filen" << endl << "w - Write your filen" << endl << "q - quit" << endl << endl;
cout << "Enter your choice: "; cin >> enterChoice;
end = processInput( start, enterChoice ); }
deleteAllRecords(start); return 0; }
bool processInput( record *&pRecords, char input[] ) { switch( input[0] ) { case 'q': { return true; }
Hej cries, Du er bare en rigtig god hjaelp. Jeg vil naerlaese din kode imorgen tidlig, da jeg er noedt til at laese til en eksamen lige nu. Der er sikkert nogle ting i din kode som jeg ikke helt forstaar og jeg kommer nok ogsaa til at spoerge om global variablen som skal hedde "debugmode" - jeg har den i min egen kode, da den er paakraevet. Ville lige sige tak for dit arbejde, saa du ikke tror jeg tager det for givet :-)
Jeg har nu kigget paa dit fine arbejde og besluttet at jeg vil bruge din loesning (da, jeg helst skal aflevere det imorgen). Kan jeg som det sidste faa dig til at skrive lidt kommentarer til dine funktioner (isaer add og delete)?? Jeg har det skidt med at aflevere noget jeg ikke forstaar. F.eks i read og write file funktionerne bruger du: in.open( filename, ios::in|ios::nocreate|ios::binary ); Det er meget nyt for mig! Jeg takker!!
struct record { char name[32]; char address[80]; char telno[15]; float gpa; record *next; };
enum { ERROR_NONE, // no errors detected ERROR_DUPLICATE, // duplicate record ERROR_NOT_FOUND, // student not found ERROR_FILE_ERROR // error reading file };
// prototypes int isRecordInList (record *pRecords, char name[] ); void deleteAllRecords(record *&pRecords); int addRecord ( record *&pRecords, char name[],char address[], char telno[], float gpa ); int readFile(record *&pRecords, char filename[]); int writeFile(record *pRecords, char filename[]); bool processInput( record *&pRecords, char input[] ); void printAll(record *pRecords); void findRecord( record *pRecords, char name[] ); int deleteRecord(record *&pRecords, char name[]); int readFile(record *&pRecords, char filename[]); int writeFile(record *pRecords, char filename[]);
int _tmain(int argc, _TCHAR* argv[]) { bool end = false; // end program ? char enterChoice[128]; // user input record *start = NULL; // records read from file or entered
<< "a - Add new student to the databasen" << endl << "f - Print information about a student in the databasen" << endl << "p - Print all information from the databasen" << endl << "d - Delete an existing student from the databasen" << endl << "r - Read your filen" << endl << "w - Write your filen" << endl << "q - quit" << endl << endl;
cout << "Enter your choice: "; cin >> enterChoice; // wait user input
end = processInput( start, enterChoice ); // process user input }
deleteAllRecords(start); // remove all records from memory return 0; }
bool processInput( record *&pRecords, char input[] ) // returns if program must quit (true) { switch( input[0] ) { case 'q': // quit, end program { return true; }
case 'r': // read file { char name[2048];
cout << "enter:" << endl; cout << " filename: "; cin >> name; // enter file name
int readFile(record *&pRecords, char filename[]) { fstream in; // in file
in.open( filename, ios::in|ios::nocreate|ios::binary ); // open file as input, dont create file if doesn't exist, read as binary data
if( !in ) // if open file failed { return ERROR_FILE_ERROR; }
deleteAllRecords( pRecords ); // remove any recoard in database
record pAddMe; // a record in.read( (char*)&pAddMe, sizeof( record ) ); // read one record from file
while( !in.eof() ) // if we didn't pass the end of the file { addRecord( pRecords, pAddMe.name, pAddMe.address, pAddMe.telno, pAddMe.gpa ); // add record to database in.read( (char*)&pAddMe, sizeof( record ) ); // read next record }
in.close(); // close file return ERROR_NONE; }
int writeFile(record *pRecords, char filename[]) { fstream out; // out file
out.open( filename, ios::out|ios::binary ); // open as output and write data as binary
if( !out ) // if open failed { return ERROR_FILE_ERROR; }
record *pHelper = pRecords; // helper pointer point at first record
while( pHelper != NULL ) // if not end of records reached (helper == NULL) { out.write( (char*)pHelper, sizeof( record ) ); // write record to file pHelper = pHelper->next; // helper points at next record in database }
out.close(); // close file return ERROR_NONE; }
void deleteAllRecords(record *&pRecords) { record *pHelper = pRecords; // helper points at first record in database
while( pRecords != NULL ) // while pRecords points at a record { pRecords = pRecords->next; // pRecords points at next record in database (helper points at the "removed" database record
delete pHelper; // delete removed database record from memory
pHelper = pRecords; // helper points at first record in database again } }
int deleteRecord(record *&pRecords, char name[]) { if( !isRecordInList( pRecords, name ) ) // if record not exist { return ERROR_NOT_FOUND; }
record *pHelper = pRecords; // helper points at first record record *pHelperLast = pRecords; // helper pointed last time at first record
while( pHelper != NULL ) // while a not end of records { if( strcmp( pHelper->name, name ) == 0 ) // is this the record to delete ? { if( pHelper == pHelperLast ) // if pHelper == pHelperLast (both point at first record in database) { pRecords = pHelper->next; // pRecords points a second record in database, hepler at first (record removed) delete pHelper; // delete first from memory } else { pHelperLast->next = pHelper->next; // pHelperLast point at record before the one to be deleted // pHelper at the one to be deleted, the rpHelperLast's next // becomes pHelper next record ( pHelper's record removed from list) delete pHelper; // delete pHelper from memory }
return ERROR_NONE; }
pHelperLast = pHelper; // last record pointed at is pHelper pHelper = pHelper->next; // pHelper advances one record forward }
return ERROR_NOT_FOUND; }
void printAll(record *pRecords) { record *pHelper = pRecords;
void findRecord( record *pRecords, char name[] ) { if( !isRecordInList( pRecords, name ) ) // if not in list { cout << "student not found!" << endl << endl; } else { record *pHelper = pRecords; // pHelper points at first record
while( pHelper != NULL ) // while pHelper didn't reach end of list { if( strcmp( pHelper->name, name ) == 0 ) // is this the record we searched for ? { break; // yes, exist while loop }
pHelper = pHelper->next; // no, was not the record we searched for, try next (advance pHelper in list) }
int addRecord ( record *&pRecords, char name[],char address[], char telno[], float gpa ) { record *pAddMe = new record; // allocate memory for new record to add
pAddMe->next = NULL; // next record NULL (end) strcpy( pAddMe->name, name ); // copy info to record strcpy( pAddMe->address, address ); strcpy( pAddMe->telno, telno ); pAddMe->gpa = gpa;
if( pRecords == NULL ) // if first record to be added (no records in database) { pRecords = pAddMe; // pRecords point to new record return ERROR_NONE; }
if( isRecordInList( pRecords, pAddMe->name ) ) // if record to be added already in list { delete pAddMe; // delete record and return error return ERROR_DUPLICATE; }
record *pHelper = pRecords; // helper points to first record in list
while( pHelper->next != NULL ) // while not at end of the list { pHelper = pHelper->next; // advance one record }
pHelper->next = pAddMe; // add new record to end of the list
return ERROR_NONE; }
int isRecordInList (record *pRecords, char name[] ) { record *pHelper = pRecords; // pHelper points at first record in list
while( pHelper != NULL ) // while pHelper doesn't point at the end of the list { if( strcmp( pHelper->name, name ) == 0 ) // the record we seacred for ? { return 1; // return is in list }
pHelper = pHelper->next; // advance to next record }
Hej Chries, mange tak for dine kommentarer:-) Naar jeg koerer min tests, virker addRecord ikke efter hensigten og den er jo naesten den vigtigste function af alle. Den tilfoejer ikke en record som den skal. Er det muligt at du kan se hvor fejlen er?? Jeg kan desvaerre ikke. Ja, jeg er ked af at ulejlige dig hele tiden og vil gerne give dig flere points end de knap 200 hvis du vil have det.
Chries, Undskyld at jeg er lidt lang tid om at vende tilbage, men jeg gaar i skole og arbejder en del af tiden. Jeg tror ikke at der er et krav du har overset. Hmmm, naar jeg compiler og vaelger "a" for add paa menuen, svarer "y" til at add en person gaar den videre og genviser menuen. Hvis jeg saa vaelger "f" paa menuen viser den: "error in input" efterfulgt af et underligt "garbage"tegn og saa f lige efter det tegn. Mystisk!
a - Add new student to the database f - Print information about a student in the database p - Print all information from the database d - Delete an existing student from the database r - Read your file w - Write your file q - quit
Enter your choice: a Please enter: name: Henning Jensen address: Fake st. #307 telno: (800)986-5432 gpa: 3.8
add this student (y/n) ? y Her siger computeren at jeg har udfoert en illegal handling. Og kan saa ikke komme videre herfra
For god ordens skyld vil jeg lige poste den kode jeg bruger. Jeg kan f.eks ikke bruge int _tmain(int argc, _TCHAR* argv[]), men derimod: int main (int argc, char* argv []))
Jeg har opdelt koden i to filer, men det skulle ikke betyde noget.
int isRecordInList (record *pRecords, char name[] ); void deleteAllRecords(record *&pRecords); int addRecord ( record *&pRecords, char name[],char address[], char telno[], float gpa ); int readFile(record *&pRecords, char filename[]); int writeFile(record *pRecords, char filename[]); bool processInput( record *&pRecords, char input[] ); void printAll(record *pRecords); void findRecord( record *pRecords, char name[] ); int deleteRecord(record *&pRecords, char name[]); int readFile(record *&pRecords, char filename[]); int writeFile(record *pRecords, char filename[]);
// global variable bool debugmode=false;
int main(int argc, char* argv[]) { record *start; // records read from file or entered start = NULL; bool end = false; // end program ? char enterChoice[128];// user input
<< "a - Add new student to the database" << endl << "f - Print information about a student in the database" << endl << "p - Print all information from the database" << endl << "d - Delete an existing student from the database" << endl << "r - Read your file" << endl << "w - Write your file" << endl << "q - quit" << endl << endl;
cout << "Enter your choice: "; cin >> enterChoice; // wait user input // process user input end = processInput( start, enterChoice );
}
deleteAllRecords(start);// remove all records from memory return 0; }
HER ER DATABASEN: #include "iostream.h" #include "string.h" #include "fstream.h"
struct record { char name[32]; char address[80]; char telno[15]; float gpa; record *next; };
enum { ERROR_NONE, // no errors detected ERROR_DUPLICATE, // duplicate record ERROR_NOT_FOUND, // student not found ERROR_FILE_ERROR // error reading file
};
//function prototypes
int isRecordInList (record *pRecords, char name[] ); void deleteAllRecords(record *&pRecords); int addRecord ( record *&pRecords, char name[],char address[], char telno[], float gpa ); int readFile(record *&pRecords, char filename[]); int writeFile(record *pRecords, char filename[]); bool processInput( record *&pRecords, char input[] ); void printAll(record *pRecords); void findRecord( record *pRecords, char name[] ); int deleteRecord(record *&pRecords, char name[]);
// returns if program must quit (true) bool processInput( record *&pRecords, char input[] ) { switch( input[0] ) { case 'q': // quit, end program
{ return true; }
case 'r': // read file { char name[2048];
cout << "enter:" << endl; cout << " filename: "; cin >> name; // enter file name
// read from file, success ? if( readFile( pRecords, name ) != ERROR_NONE )
return true; } //function readfile reads data from file, copies to buffer, //displays
int readFile(record *&pRecords, char filename[]) { fstream in;// in file
// open file as input, dont create file if doesn't exist, read as binary data in.open( filename, ios::in|ios::nocreate|ios::binary );
if( !in )// if open file failed { return ERROR_FILE_ERROR; }
deleteAllRecords( pRecords );// remove any recoard in database
record pAddMe;// a record
// read one record from file in.read( (char*)&pAddMe, sizeof( record ) );
while( !in.eof() )// if we didn't pass the end of the file
{ // add record to database addRecord( pRecords, pAddMe.name, pAddMe. address, pAddMe.telno, pAddMe.gpa ); //read next record in.read( (char*)&pAddMe, sizeof( record ) ); }
in.close();//close the file return ERROR_NONE; } // Function writefile prints to file int writeFile(record *pRecords, char filename[]) { fstream out; // open as output and write data as binary out.open( filename, ios::out|ios::binary );
if( !out )// if open failed
{ return ERROR_FILE_ERROR; }
record *pHelper = pRecords;//helper pointer point at first record
while( pHelper != NULL )// if not end of records //reached (helper == NULL)
{ out.write( (char*)pHelper, sizeof( record ) );// write record to file pHelper = pHelper->next;// helper points at next //record in database }
out.close();//close the file return ERROR_NONE; } //deleteAllRecords will delete void deleteAllRecords(record *&pRecords) { record *pHelper = pRecords;// helper points at //first record in database
while( pRecords != NULL )// while pRecords points at a record
{ pRecords = pRecords->next;// pRecords points at next //record in database
delete pHelper;// delete removed database record from memory
pHelper = pRecords;// helper points at first record //in database again } } // deleteStudent prompts the user for a student's name and // will delete only the first matching record based on the name // in case they are duplicates (by accident) int deleteRecord(record *&pRecords, char name[]) { if( !isRecordInList( pRecords, name ) ) // if record not exist { return ERROR_NOT_FOUND; }
record *pHelper = pRecords; // helper points at first record record *pHelperLast = pRecords;// helper pointed last time at //first record
while( pHelper != NULL ) // while a not end of records { if( strcmp( pHelper->name, name ) == 0 )// is this the //record to delete ? { if( pHelper == pHelperLast )// if pHelper == pHelperLast // (both point at first record in database) { pRecords = pHelper->next;// pRecords points a second //record in database, hepler //at first (record removed) delete pHelper; // delete first from memory } else { // pHelperLast point at record before the one to be deleted // pHelper at the one to be deleted, the rpHelperLast's next // becomes pHelper next record ( pHelper's record removed // from list) pHelperLast->next = pHelper->next; delete pHelper;// delete pHelper from memory }
return ERROR_NONE; }
pHelperLast = pHelper; // last record pointed at is pHelper pHelper = pHelper->next;// pHelper advances one record forward }
return ERROR_NOT_FOUND; }
void printAll(record *pRecords) { record *pHelper = pRecords;
void findRecord( record *pRecords, char name[] ) { if( !isRecordInList( pRecords, name ) )// if not in list { cout << "student not found!" << endl << endl; } else { record *pHelper = pRecords;// pHelper points at first record
while( pHelper != NULL )// while pHelper didn't reach end of // list { // is this the record we searched for ? if( strcmp( pHelper->name, name ) == 0 ) { break;// yes, exist while loop }
pHelper = pHelper->next;// no, was not the record we // searched for, try next (advance pHelper in list) }
int addRecord ( record *&pRecords, char name[],char address[], char telno[], float gpa ) { record *pAddMe = new record;// allocate memory for new //record to add
pAddMe->next = NULL; // next record NULL (end) strcpy( pAddMe->name, name );// copy info to record strcpy( pAddMe->address, address ); strcpy( pAddMe->telno, telno ); pAddMe->gpa = gpa;
if( pRecords == NULL )// if first record to be added //(no records in database) { pRecords = pAddMe; // pRecords point to new record return ERROR_NONE; } // if record to be added already in list if( isRecordInList( pRecords, pAddMe->name ) ) { delete pAddMe; // delete record and return error return ERROR_DUPLICATE; } //helper points to first record in list record *pHelper = pRecords;
while( pHelper->next != NULL ) // while not at end of the list { pHelper = pHelper->next; // advance one record }
pHelper->next = pAddMe; // add new record to end of the list
return ERROR_NONE; }
int isRecordInList (record *pRecords, char name[] ) { //pHelper points at first record in list record *pHelper = pRecords;
// while pHelper doesn't point at the end of the list while( pHelper != NULL ) { //the record we seacred for ? if( strcmp( pHelper->name, name ) == 0 ) { return 1; // return is in list }
pHelper = pHelper->next;//advance to next record }
det er din modificering i add student der ødelægger det, den søger ikke for at nulk terminere strenge eller ignorer input helt. hvis du ændrer det til:
hvis du bruger den struct du har lavet skal du huske at modificere: if( input[0] == 'y' )// if yes { // add student, success ? if( addRecord( pRecords, name, address, telno, gpa ) != ERROR_NONE ) { cout << "student exists" << endl; } }
Ja, nu koerer det soerme. Der er kun et par smaating, men jeg vil ikke ulejlige dig mere med det. Mange tak for din hjaelp. Du faar som fortjent dine points (du fortjener meget mere). Har afsat 10 points til laffe, da det er min politik altid at saette af til de der giver sig tid til at kigge paa mine spoergsmaal.
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.