Avatar billede henrik10 Nybegynder
28. april 2002 - 03:07 Der 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


#include <iostream.h>
#include <string>
#include <fstream.h>
//#include "finalprojectDBAccess.cpp"




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(argc == 1)
        debugmode = false;
    else if(argc == 2 && strcmp(argv[1], "debug") == 0)
        debugmode = true;
    else
        cout << "Error" << endl;


    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;

    for(int j=0; j<numstudents; ++j)
    {
        cout << "Name: " << flush;
        cin.ignore (100,'\n');
        cin >> start[j].name,32;
        cout << "Address: " << flush;
        cin.ignore (100,'\n');
        cin >> start[j].address, 80;
        cout << "Phone no: " << flush;
        cin.ignore (100,'\n');
        cin >> start[j].telno, 15;
        cout << "GPA: " << flush;
        cin.ignore (100,'\n');
        cin >> start[j].gpa;
    }

   
    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[])
{
}
Avatar billede chries Nybegynder
28. april 2002 - 12:08 #1
i følgende funktion mangler parameter navne:
int addRecord (record*&, char[],char[],char[], float) 
void findRecord(record*, char name[])
void printAll(record*)
int readFile(record Stud*&, char filename[])
int writeFile(record Stud*, char filename[])
int isRecordInList (record *, char[])
Avatar billede laffe Nybegynder
28. april 2002 - 17:48 #2
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.
}
Avatar billede laffe Nybegynder
28. april 2002 - 18:06 #3
Jeg har igen ændret lidt på parameter navnene.

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.
    }
  }
}
Avatar billede laffe Nybegynder
28. april 2002 - 18:09 #4
Her er den næste funktion.

void findRecord(record* liste, char* name)
{
  record* ptr=liste;

  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;
  }
}
Avatar billede laffe Nybegynder
28. april 2002 - 18:11 #5
Her er den næste funktion.

void printAll(record*)
{
  record* ptr=liste;

  while (ptr)
  {
    //.... udskriv alle data. ptr peger på den aktuelle record
    }
    ptr->next;
  }
  cout<<"The following information has been printed"<<endl;
}
Avatar billede laffe Nybegynder
28. april 2002 - 19:32 #6
Sikke noget lort jeg har fået lavet. Det virker ikke. Ignorer lige mine svar indtil videre.

Sorry.
Avatar billede chries Nybegynder
28. april 2002 - 22:15 #7
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.
//

#include "stdafx.h"
#include "iostream.h"
#include "fstream.h"

struct record
{
  char name[32];
  char address[80];
  char telno[15];
  float gpa;
  record *next;
};

enum
{
    ERROR_NONE,
    ERROR_DUPLICATE,
    ERROR_NOT_FOUND,
    ERROR_FILE_ERROR
};

bool isRecordInList (record *pRecords, char name[] );
void deleteAllRecords(record *&pRecords);
int addRecord ( record *&pRecords, record *pAddMe );
int readFile(record *&pRecords, char filename[]);
int writeFile(record *pRecords, char filename[]);
bool processInput( record *&pRecords, char input[] );
void printAll(record *pRecords);
record* 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;
    char enterChoice[128];
    record *pRecords = NULL;

    while( !end )
    {
        cout << "              MENU" << endl
            << "              ****" << endl << endl

            << "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;
        }

        case 'r':
        {
            char name[2048];

            cout << "enter:" << endl;
            cout << " filename:    ";
            cin >> name;

            if( readFile( pRecords, name ) != ERROR_NONE )
            {
                cout << "file error !!" << endl << endl;
            }

            return false;
        }

        case 'w':
        {
            char name[2048];

            cout << "enter:" << endl;
            cout << "  filename:    ";
            cin >> name;

            if( writeFile( pRecords, name ) != ERROR_NONE )
            {
                cout << "file error !!" << endl << endl;
            }

            return false;
        }

        case 'd':
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;

            if( deleteRecord( pRecords, name ) != ERROR_NONE )
            {
                cout << "student not found!" << endl << endl;
            }

            return false;
        }

        case 'p':
        {
            printAll(pRecords);
            return false;
        }

        case 'f':
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;

            record *pFound = findRecord( pRecords, name );
           
           
            if( pFound )
            {
                cout << "record:" << endl;
                cout << "  name:    " << pFound->name << endl;
                cout << "  address: " << pFound->address << endl;
                cout << "  telno:  " << pFound->telno << endl;
                cout << "  gpa:    " << pFound->gpa << endl;
                cout << endl;
            }
            else cout << "student not found!" << endl << endl;

            return false;
        }

        case 'a':
        {
            record *pAddMe = new record;

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> pAddMe->name;

            cout << "  address: ";
            cin >> pAddMe->address;

            cout << "  telno:  ";
            cin >> pAddMe->telno;

            cout << "  gpa:    ";
            cin >> pAddMe->gpa;

            pAddMe->next = NULL;

            cout << endl << "add this student (y/n) ? ";
            cin >> input;

            if( input[0] == 'y' )
            {
                if( addRecord( pRecords, pAddMe ) != ERROR_NONE )
                {
                    cout << "student exists" << endl;
                    delete pAddMe;
                }           
            }
            else
            {
                cout << "student not added!" << endl << endl;
                delete pAddMe;
            }

            return false;
        }

        default:
        {
            cout << endl << "error in input: " << input << endl << endl;
            return false;
        }
    }

    return true;
}

int readFile(record *&pRecords, char filename[])
{
    fstream in;

    in.open( filename, ios::in|ios::nocreate|ios::binary );

    if( !in )
    {
        return ERROR_FILE_ERROR;
    }

    deleteAllRecords( pRecords );

    record *pAddMe = new record;
    in.read( (char*)pAddMe, sizeof( record ) );

    while( !in.eof() )
    {
        addRecord( pRecords, pAddMe );

        pAddMe = new record;
        in.read( (char*)pAddMe, sizeof( record ) );
    }

    delete pAddMe;

    in.close();
    return ERROR_NONE;
}

int writeFile(record *pRecords, char filename[])
{
    fstream out;

    out.open( filename, ios::out|ios::binary );

    if( !out )
    {
        return ERROR_FILE_ERROR;
    }

    record *pHelper = pRecords;
   
    while( pHelper != NULL )
    {
        out.write( (char*)pHelper, sizeof( record ) );
        pHelper = pHelper->next;
    }

    out.close();
    return ERROR_NONE;
}

void deleteAllRecords(record *&pRecords)
{
    record *pHelper = pRecords;
   
    while( pRecords != NULL )
    {
        pRecords = pRecords->next;

        delete pHelper;

        pHelper = pRecords;
    }
}

int deleteRecord(record *&pRecords, char name[])
{
    if( !isRecordInList( pRecords, name ) )
    {
        return ERROR_NOT_FOUND;
    }
   
    record *pHelper = pRecords;
    record *pHelperLast = pRecords;
   
    while( pHelper != NULL )
    {
        if( strcmp(  pHelper->name, name ) == 0 )
        {
            if( pHelper == pHelperLast )
            {
                pRecords = pHelper->next;
                delete pHelper;
            }
            else
            {
                pHelperLast->next = pHelper->next;
                delete pHelper;
            }

            return ERROR_NONE;
        }
       
        pHelperLast = pHelper;
        pHelper = pHelper->next;
    }

    return ERROR_NOT_FOUND;
}


void printAll(record *pRecords)
{
    record *pHelper = pRecords;
   
    while( pHelper != NULL )
    {
        cout << "record:" << endl;
        cout << "  name:    " << pHelper->name << endl;
        cout << "  address: " << pHelper->address << endl;
        cout << "  telno:  " << pHelper->telno << endl;
        cout << "  gpa:    " << pHelper->gpa << endl;
        cout << endl;

        pHelper = pHelper->next;
    }   
}

record* findRecord( record *pRecords, char name[] )
{
    if( !isRecordInList( pRecords, name ) )
    {
        return NULL;
    }

    record *pHelper = pRecords;
   
    while( pHelper != NULL )
    {
        if( strcmp(  pHelper->name, name ) == 0 )
        {
            break;
        }

        pHelper = pHelper->next;
    }

    return pHelper;
}

int addRecord ( record *&pRecords, record *pAddMe )
{
    pAddMe->next = NULL;

    if( pRecords == NULL )
    {
        pRecords = pAddMe;
        return ERROR_NONE;
    }

    if( isRecordInList( pRecords, pAddMe->name ) )
    {
        return ERROR_DUPLICATE;
    }

    record *pHelper = pRecords;
   
    while( pHelper->next != NULL )
    {
        pHelper = pHelper->next;
    }

    pHelper->next = pAddMe;

    return ERROR_NONE;
}

bool isRecordInList (record *pRecords, char name[] )
{
    record *pHelper = pRecords;
   
    while( pHelper != NULL )
    {
        if( strcmp(  pHelper->name, name ) == 0 )
        {
            return true;
        }

        pHelper = pHelper->next;
    }
   
    return false;
}
Avatar billede henrik10 Nybegynder
29. april 2002 - 10:45 #8
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!
Avatar billede chries Nybegynder
29. april 2002 - 10:48 #9
ok, jeg har dog lige et spørgsmål, hvad vil du bruge:
void findRecord(record*, char name[]);

til. Den kan finde en record, men ikke returnere det den har fundt !
Avatar billede henrik10 Nybegynder
29. april 2002 - 11:22 #10
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.
Avatar billede chries Nybegynder
29. april 2002 - 11:29 #11
jaaah :-) jeg troede den var til at finde en student og returner informationer :-)
Avatar billede chries Nybegynder
29. april 2002 - 11:53 #12
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[]);
*/

#include "stdafx.h"
#include "iostream.h"
#include "fstream.h"

struct record
{
  char name[32];
  char address[80];
  char telno[15];
  float gpa;
  record *next;
};

enum
{
    ERROR_NONE,
    ERROR_DUPLICATE,
    ERROR_NOT_FOUND,
    ERROR_FILE_ERROR
};

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;
    char enterChoice[128];
    record *start = NULL;

    while( !end )
    {
        cout << "              MENU" << endl
            << "              ****" << endl << endl

            << "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;
        }

        case 'r':
        {
            char name[2048];

            cout << "enter:" << endl;
            cout << " filename:    ";
            cin >> name;

            if( readFile( pRecords, name ) != ERROR_NONE )
            {
                cout << "file error !!" << endl << endl;
            }

            return false;
        }

        case 'w':
        {
            char name[2048];

            cout << "enter:" << endl;
            cout << "  filename:    ";
            cin >> name;

            if( writeFile( pRecords, name ) != ERROR_NONE )
            {
                cout << "file error !!" << endl << endl;
            }

            return false;
        }

        case 'd':
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;

            if( deleteRecord( pRecords, name ) != ERROR_NONE )
            {
                cout << "student not found!" << endl << endl;
            }

            return false;
        }

        case 'p':
        {
            printAll(pRecords);
            return false;
        }

        case 'f':
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;

            findRecord( pRecords, name );
                     
            return false;
        }

        case 'a':
        {
            char name[128];
            char address[128];
            char telno[128];
            float gpa;

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;

            cout << "  address: ";
            cin >> address;

            cout << "  telno:  ";
            cin >> telno;

            cout << "  gpa:    ";
            cin >> gpa;

            cout << endl << "add this student (y/n) ? ";
            cin >> input;

            if( input[0] == 'y' )
            {
                if( addRecord( pRecords, name, address, telno, gpa ) != ERROR_NONE )
                {
                    cout << "student exists" << endl;
                }           
            }
            else
            {
                cout << "student not added!" << endl << endl;
            }

            return false;
        }

        default:
        {
            cout << endl << "error in input: " << input << endl << endl;
            return false;
        }
    }

    return true;
}

int readFile(record *&pRecords, char filename[])
{
    fstream in;

    in.open( filename, ios::in|ios::nocreate|ios::binary );

    if( !in )
    {
        return ERROR_FILE_ERROR;
    }

    deleteAllRecords( pRecords );

    record pAddMe;
    in.read( (char*)&pAddMe, sizeof( record ) );

    while( !in.eof() )
    {
        addRecord( pRecords, pAddMe.name, pAddMe.address, pAddMe.telno, pAddMe.gpa );
        in.read( (char*)&pAddMe, sizeof( record ) );
    }

    in.close();
    return ERROR_NONE;
}

int writeFile(record *pRecords, char filename[])
{
    fstream out;

    out.open( filename, ios::out|ios::binary );

    if( !out )
    {
        return ERROR_FILE_ERROR;
    }

    record *pHelper = pRecords;
   
    while( pHelper != NULL )
    {
        out.write( (char*)pHelper, sizeof( record ) );
        pHelper = pHelper->next;
    }

    out.close();
    return ERROR_NONE;
}

void deleteAllRecords(record *&pRecords)
{
    record *pHelper = pRecords;
   
    while( pRecords != NULL )
    {
        pRecords = pRecords->next;

        delete pHelper;

        pHelper = pRecords;
    }
}

int deleteRecord(record *&pRecords, char name[])
{
    if( !isRecordInList( pRecords, name ) )
    {
        return ERROR_NOT_FOUND;
    }
   
    record *pHelper = pRecords;
    record *pHelperLast = pRecords;
   
    while( pHelper != NULL )
    {
        if( strcmp(  pHelper->name, name ) == 0 )
        {
            if( pHelper == pHelperLast )
            {
                pRecords = pHelper->next;
                delete pHelper;
            }
            else
            {
                pHelperLast->next = pHelper->next;
                delete pHelper;
            }

            return ERROR_NONE;
        }
       
        pHelperLast = pHelper;
        pHelper = pHelper->next;
    }

    return ERROR_NOT_FOUND;
}


void printAll(record *pRecords)
{
    record *pHelper = pRecords;
   
    while( pHelper != NULL )
    {
        cout << "record:" << endl;
        cout << "  name:    " << pHelper->name << endl;
        cout << "  address: " << pHelper->address << endl;
        cout << "  telno:  " << pHelper->telno << endl;
        cout << "  gpa:    " << pHelper->gpa << endl;
        cout << endl;

        pHelper = pHelper->next;
    }   
}

void findRecord( record *pRecords, char name[] )
{
    if( !isRecordInList( pRecords, name ) )
    {
        cout << "student not found!" << endl << endl;
    }
    else
    {
        record *pHelper = pRecords;
       
        while( pHelper != NULL )
        {
            if( strcmp(  pHelper->name, name ) == 0 )
            {
                break;
            }

            pHelper = pHelper->next;
        }   

        cout << "record:" << endl;
        cout << "  name:    " << pHelper->name << endl;
        cout << "  address: " << pHelper->address << endl;
        cout << "  telno:  " << pHelper->telno << endl;
        cout << "  gpa:    " << pHelper->gpa << endl;
        cout << endl;
    }
}

int addRecord ( record *&pRecords, char name[],char address[], char telno[], float gpa )
{   
    record *pAddMe = new record;
   
    pAddMe->next = NULL;
    strcpy( pAddMe->name, name );
    strcpy( pAddMe->address, address );
    strcpy( pAddMe->telno, telno );
    pAddMe->gpa = gpa;

    if( pRecords == NULL )
    {
        pRecords = pAddMe;
        return ERROR_NONE;
    }

    if( isRecordInList( pRecords, pAddMe->name ) )
    {
        delete pAddMe;
        return ERROR_DUPLICATE;
    }

    record *pHelper = pRecords;
   
    while( pHelper->next != NULL )
    {
        pHelper = pHelper->next;
    }

    pHelper->next = pAddMe;

    return ERROR_NONE;
}

int isRecordInList (record *pRecords, char name[] )
{
    record *pHelper = pRecords;
   
    while( pHelper != NULL )
    {
        if( strcmp(  pHelper->name, name ) == 0 )
        {
            return 1;
        }

        pHelper = pHelper->next;
    }
   
    return 0;
}
Avatar billede henrik10 Nybegynder
29. april 2002 - 22:34 #13
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 :-)
Avatar billede henrik10 Nybegynder
30. april 2002 - 11:53 #14
Hej Cries,

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!!
Avatar billede chries Nybegynder
30. april 2002 - 13:02 #15
har smidt kommentarer på, hvis du skal have flere , sig endelig til

// TestMe4.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "iostream.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
};

// 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

    while( !end )                // while ! exit program
    {
        cout << "              MENU" << endl
            << "              ****" << endl << endl

            << "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

            if( readFile( pRecords, name ) != ERROR_NONE )    // read from file, success ?
            {
                cout << "file error !!" << endl << endl;    // failure reading file
            }

            return false;        // don't quit
        }

        case 'w':                // write file
        {
            char name[2048];

            cout << "enter:" << endl;
            cout << "  filename:    ";
            cin >> name;        // enter file name

            if( writeFile( pRecords, name ) != ERROR_NONE )    // write to file, success ?
            {
                cout << "file error !!" << endl << endl;
            }

            return false;
        }

        case 'd':                // delete student
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;        // enter name

            if( deleteRecord( pRecords, name ) != ERROR_NONE )    // delete student , success ?
            {
                cout << "student not found!" << endl << endl;   
            }

            return false;
        }

        case 'p':                // print database
        {
            printAll(pRecords);
            return false;
        }

        case 'f':                // print student information
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;        // enter student name

            findRecord( pRecords, name );
                     
            return false;
        }

        case 'a':                // add student
        {
            char name[128];
            char address[128];
            char telno[128];
            float gpa;

            cout << "enter:" << endl;    // enter student info
            cout << "  name:    ";
            cin >> name;

            cout << "  address: ";
            cin >> address;

            cout << "  telno:  ";
            cin >> telno;

            cout << "  gpa:    ";
            cin >> gpa;

            cout << endl << "add this student (y/n) ? ";
            cin >> input;                // add student ?

            if( input[0] == 'y' )        // if yes
            {
                if( addRecord( pRecords, name, address, telno, gpa ) != ERROR_NONE ) // add student, success ?
                {
                    cout << "student exists" << endl;
                }           
            }
            else                        // no, dont add student
            {
                cout << "student not added!" << endl << endl;
            }

            return false;
        }

        default:                        // user input error
        {
            cout << endl << "error in input: " << input << endl << endl;
            return false;
        }
    }

    return true;                       
}

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;
   
    while( pHelper != NULL )
    {
        cout << "record:" << endl;
        cout << "  name:    " << pHelper->name << endl;
        cout << "  address: " << pHelper->address << endl;
        cout << "  telno:  " << pHelper->telno << endl;
        cout << "  gpa:    " << pHelper->gpa << endl;
        cout << endl;

        pHelper = pHelper->next;
    }   
}

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)
        }   

        cout << "record:" << endl;
        cout << "  name:    " << pHelper->name << endl;
        cout << "  address: " << pHelper->address << endl;
        cout << "  telno:  " << pHelper->telno << endl;
        cout << "  gpa:    " << pHelper->gpa << endl;
        cout << endl;
    }
}

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
    }
   
    return 0;                                    // not in list
}
Avatar billede henrik10 Nybegynder
01. maj 2002 - 12:04 #16
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.
Avatar billede chries Nybegynder
01. maj 2002 - 12:18 #17
Jeg forstår ikke helt. Hvad mener du med den ikke tilføjer efter hensigten ?

I debuggeren kan jeg se den ikke opretter, hvis personen allerede existerer. Og jeg kan se den putter den bag i listen hver gang.

Er der et krav jeg har overset ?
Avatar billede henrik10 Nybegynder
01. maj 2002 - 23:20 #18
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!
Avatar billede henrik10 Nybegynder
01. maj 2002 - 23:42 #19
Har lige kopieret mit out put         
              MENU
              ****

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
Avatar billede henrik10 Nybegynder
01. maj 2002 - 23:49 #20
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.

Her er main:

#include "iostream.h"
#include "FinalDBAccess.cpp"
#include "string.h"
#include "fstream.h"


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
   

    if(argc == 1)
        debugmode = false;
    else if(argc == 2 && strcmp(argv[1], "debug") == 0)
        debugmode = true;
    else
        cout << "Error" << endl;


    if(debugmode)
        cout << "If debugmode is true, print debug-information!"<< endl;
   

    while( !end ) // while ! exit program
   
    {
        cout << "              MENU" << endl
            << "              ****" << endl << endl

            << "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 )
                                                       
           
            {
                //failure reading file
                cout << "file error !!" << endl << endl;
            }

            return false; //don't quit
        }

        case 'w':
        {
            char name[2048];

            cout << "enter:" << endl;
            cout << "  filename:    ";
            cin >> name;// enter file name

            // write to file, success ?
            if( writeFile( pRecords, name ) != ERROR_NONE )
            {
                cout << "file error !!" << endl << endl;
            }

            return false;
        }

        case 'd': // delete student
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;// enter name

            // delete student , success ?
            if( deleteRecord( pRecords, name ) != ERROR_NONE )
            {
                cout << "student not found!" << endl << endl;
            }

            return false;
        }

        case 'p':// print database
        {
            printAll(pRecords);
            return false;
        }

        case 'f': // print student information
       
        {
            char name[128];

            cout << "enter:" << endl;
            cout << "  name:    ";
            cin >> name;// enter student name


            findRecord( pRecords, name );
                     
            return false;
        }

        case 'a':// add student
       
        {
            record uptouser[80];
            char name[128];
            char address[128];
            char telno[128];
            float gpa;

            cout << "Please enter:" << endl;// enter student info
            cout << "  name:    ";
            cin.ignore (100, '\n');
            cin >> uptouser[0].name,32;

            cout << "  address: ";
            cin.ignore (100, '\n');
            cin >> uptouser[0].address,32;

            cout << "  telno:  ";
            cin.ignore (100, '\n');
            cin >> uptouser[0].telno,32;

            cout << "  gpa:    ";
            cin >> gpa;
           

            cout << endl << "add this student (y/n) ? ";
            cin >> input; // add student?
           


            if( input[0] == 'y' )// if yes
            {
                // add student, success ?
                if( addRecord( pRecords, name, address, telno, gpa ) != ERROR_NONE )
                {
                    cout << "student exists" << endl;
                }           
            }
            else    // no, dont add student
            {
                cout << "student has not been added!" << endl << endl;
            }

            return false;
        }

        default:    //user input error
        {
            cout << endl << "error in input: " << input << endl << endl;
            return false;
        }
    }

    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;
   
    while( pHelper != NULL )
    {
        cout << "record:" << endl;
        cout << "  name:    " << pHelper->name << endl;
        cout << "  address: " << pHelper->address << endl;
        cout << "  telno:  " << pHelper->telno << endl;
        cout << "  gpa:    " << pHelper->gpa << endl;
        cout << endl;

        pHelper = pHelper->next;
    }   
}

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)
        }   

        cout << "record:" << endl;
        cout << "  name:    " << pHelper->name << endl;
        cout << "  address: " << pHelper->address << endl;
        cout << "  telno:  " << pHelper->telno << endl;
        cout << "  gpa:    " << pHelper->gpa << endl;
        cout << endl;
    }
}

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
    }
   
    return 0;// not in list
}
Avatar billede chries Nybegynder
02. maj 2002 - 09:32 #21
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:

            char name[128];
            char address[128];
            char telno[128];
            float gpa;

            cout << "Please enter:" << endl;// enter student info
            cout << "  name:    ";
            cin >> name;

            cout << "  address: ";
            cin >> address;

            cout << "  telno:  ";
            cin >> telno;

            cout << "  gpa:    ";
            cin >> gpa;
           

            cout << endl << "add this student (y/n) ? ";
            cin >> input; // add student?


kører det fint
Avatar billede chries Nybegynder
02. maj 2002 - 09:32 #22
ikke søger, sørger (eller hvordan det staves =)
Avatar billede chries Nybegynder
02. maj 2002 - 09:33 #23
..null terminere..
Avatar billede chries Nybegynder
02. maj 2002 - 09:40 #24
ups, der var jeg for hurtig :-)

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;
                }           
            }
Avatar billede henrik10 Nybegynder
02. maj 2002 - 10:04 #25
Du er altsaa en kanon hjaelp.
jeg modificerede det, for naar man skal indtaste sit navn skal der vaere plads til fornavn og efternavn.
Avatar billede henrik10 Nybegynder
02. maj 2002 - 10:47 #26
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.
Avatar billede Ny bruger Nybegynder

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.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester