Results 1 to 7 of 7

Thread: Whats wrong with this code?

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    40

    Whats wrong with this code?

    I cant get it to display the contents in assign6 file. Its supposed to order whats already in the file and save it back out to disk.

    In the file it says

    Smith, Bob
    233-444-5555
    43
    Manager




    PHP Code:
    code:--------------------------------------------------------------------------------
    #include "stdafx.h"
    #include <fstream>
    #include <iostream>

    using namespace std;


    struct record
    {
        
    char name[50];
        
    char ssn[12];
        
    char age[4];
        
    char occupation[50];
        
    struct record *next/* pointer to next entry */
        
    struct record *prior/* pointer to previous record */
      
    };


    struct record *start;  /* pointer to first entry in list */
    struct record *last;  /* pointer to last entry */
    struct record *find(char *);

    void enter(void), search(void), save(void);
    void load(void), list(void);
    void dls_store(struct record *istruct record **start,
                   
    struct record **last);

    void inputs(char *, char *, int), display(struct record *);




    int menu();


    class 
    LL 
    {
    private:
    record *head;
    record *tail;
    public:
    LL();
    int insert(record data);
    record *search(char[]);
    int delete_item(char[]);

    };
    LL::LL ( )
    {
    head NULL;
    tail NULL;
    }


    int main(int argccharargv[])
    {
        
    load();
         
    start last NULL;  /* initialize start and end pointers */
        
    int temp =0;

    do {    
        
        
    temp menu ();
        switch(
    temp)
        {

          case 
    1: list();
            break;
         
          case 
    4save(); 
            break;

          default:
              
    cout << temp << " is invalid!" << endl;
              }
    } while (
    temp != 4);

    return 
    0;

    }





    /* Create a doubly linked list in sorted order. */
    void dls_store(
      
    struct record *i,   /* new element */
      
    struct record **start/* first element in list */
      
    struct record **last /* last element in list */
    )
    {
      
    struct record *old, *p;

      if(*
    last==NULL) {  /* first element in list */
        
    i->next NULL;
        
    i->prior NULL;
        *
    last i;
        *
    start i;
        return;
      }
      
    = *start/* start at top of list */

      
    old NULL;
      while(
    p) {
        if(
    strcmp(p->namei->name)<0){
          
    old p;
          
    p->next;
        }
        else {
          if(
    p->prior) {
            
    p->prior->next i;
            
    i->next p;
            
    i->prior p->prior;
            
    p->prior i;
            return;
          }
          
    i->next p/* new first element */
          
    i->prior NULL;
          
    p->prior i;
          *
    start i;
          return;
        }
      }
      
    old->next i/* put on end */
      
    i->next NULL;
      
    i->prior old;
      *
    last i;
    }


    /* Display the entire list. */
    void list(void)
    {
      
    struct record *info;

      
    info start;
      while(
    info) {
        
    display(info);
        
    info info->next;  /* get next address */
      
    }
      
    printf("\n\n");
    }

    /* This function actually prints the fields in each address. */
    void display(struct record *info)
    {
        
    printf("%s\n"info->name);
        
    printf("%s\n"info->ssn);
        
    printf("%s\n"info->age);
        
    printf("%s\n"info->occupation);
        
    printf("\n\n");
    }

    /* Load the address file. */
    void load()
    {
      
    struct record *info;
      
    FILE *fp;

      
    fp fopen("assign6""rb");
      if(!
    fp) {
        
    printf("Cannot open file.\n");
        exit(
    1);
      }

      
    /* free any previously allocated memory */
      
    while(start) {
        
    info start->next;
        
    free(info);
        
    start info;
      }

      
    /* reset top and bottom pointers */
      
    start last NULL;

      
    printf("\nLoading File\n");
      while(!
    feof(fp)) {
        
    info = (struct record *) malloc(sizeof(struct record));
        if(!
    info) {
          
    printf("Out of Memory");
          return;
        }
        if(
    != fread(infosizeof(struct record), 1fp)) break;
        
    dls_store(info, &start, &last);
      }
      
    fclose(fp);
    }


    /* Save the file to disk. */
    void save(void)
    {
      
    struct record *info;

      
    FILE *fp;

      
    fp fopen("assign6""wb");
      if(!
    fp) {
        
    printf("Cannot open file.\n");
        exit(
    1);
      }
      
    printf("\nSaving File\n");

      
    info start;
      while(
    info) {
        
    fwrite(infosizeof(struct record), 1fp);
        
    info info->next;  /* get next address */
      
    }
      
    fclose(fp);
    }


    int menu ()
    {
        
    int item;
        
    // display menu and return output

        
    cout << "===============================" << endl;
        
    cout << "      Please Select An Option            " << endl << endl;
        
    cout << " 1. Display a record" << endl;
        
    cout << " 2. Add a record" << endl;
        
    cout << " 3. delete a record" << endl;
        
    cout << " 4. Exit" << endl;
        
        
        
    cin >> item;
        
    cin.ignore ();

        
    // return item
        
    return item;
    }
    -------------------------------------------------------------------------------- 
    I think i might need to put this part
    void dls_store(

    in the class somehow?

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Is this supposed to be C or C++?

    Why are you implementing your own linked list? What's wrong with the Standard one?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    40
    its c++ dude. What standard one are u talking about?

    This is for an project in college. Check my post below

    What in my code makes it C? What can I change to make it C++? Man im confused
    Last edited by havoq93; Oct 28th, 2002 at 02:56 PM.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You're mixing a lot between printf, fopen, etc. (C) and cout, ifstream, etc. (C++).

    The Standard C++ Library list<> template is the one I'm talking about. For your "record" structure, I'd recommend using a library string as well.

    Take a look round the forum (search) for more info on those.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    40
    well parksie, as much easier as that prob is, We havnt reached that step in our class...I dont think we can go and use library strings and whatnot, cuz it doesnt say that on our project sheet. CHances are, if its an easier method, then our teacher doesnt want it. LOL

    For now, I just gotta figure out this dam problem before tommorrow when its due!

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    *sigh*

    The whole point of C++ is that you learn how to use strings, lists, and things like that *before* you know all the intricacies of pointers and character arrays and whatnot.

    I say it again, most programming courses are horrific.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    40
    Ya I know, it sux

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width