Thread: Whats wrong with this code?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    19

    Unhappy 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



    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 *i, struct 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 argc, char* argv[])
    {
    	load();
    	 start = last = NULL;  /* initialize start and end pointers */
    	int temp =0;
    
    do {	
    	
    	temp = menu ();
        switch(temp)
    	{
    
          case 1: list();
    	    break;
         
          case 4: save(); 
            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;
      }
      p = *start; /* start at top of list */
    
      old = NULL;
      while(p) {
        if(strcmp(p->name, i->name)<0){
          old = p;
          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(1 != fread(info, sizeof(struct record), 1, fp)) 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(info, sizeof(struct record), 1, fp);
        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
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    If your code were in C++, I'd say, offhand, that you went overboard with the keyword 'struct'.

    I'm not familiar enough with C to know whether, or not, a user-defined type such as 'record' needs to be preceded by "struct".

    Perhaps if you posted this on the C Board, someone could be more helpful than I.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  3. #3
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    whoa!

    i guess im still in the simple leagues here.

    thats a DOS program right?

    cuz you dont have #include <windows.h> right? i dont know. but id rather program in Winows than DOS. but DOS is a start for me.

    i wont go any furthur right now.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    my code isnt c++ code? WTF? Im confused

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> my code isnt c++ code? WTF? Im confused

    Well the fact that you've used classes and namespaces is a clear indication that your code is C++ code.

  6. #6
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    whoa! i thought it was too. thank god i got a book saying im using C++ (hehehehe)

    and apparently the fact your using cout makes it C++ (i heard about that like 20 minutes ago)

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    OK well, anywayz, anyone help out?

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Do you get any output? If so, what is it?

    >> char ssn[12];
    Try char ssn[13];

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    I dont get any output in the file. Let alone the display.

    Its blank.


    Also, does it matter if my printf statements and fopen are from C? Shoudnt I be using cout, ifstream etc?

    Why is there no output? I think mabe its cuz of the void dls_store(
    not being apart of the class I dunno

  10. #10
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    fopen? fprint? whoa. i just use cout and cin.
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  11. #11
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Why do you have a class when you don't even use it?

  12. #12
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    hey i noticed that too!
    why?
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    Originally posted by The Dog
    Why do you have a class when you don't even use it?
    Well, Im supposed to be using it, I just dont know how exactly and where to input it so it works. Mabe some of you guys could fix some of my code so it's proper and may even work. I gotta get this dam project done by tommorrow cuz its due!

    My last post was here
    http://cboard.cprogramming.com/showt...threadid=27266

    But Ive figured out alot of the code, I just need help

    Thanx guys

  14. #14
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    well i cant help cuz im still in the basics. i just learned how to jump from fucntion to function. i started to learn structures and classes but i put down the book for the night. im gonna try some more programs today when i get home!
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    >I gotta get this dam project done by tommorrow cuz its due!

    While I'm reluctant to take the hard-line stance that some of the more senior members do - Prelude, Hammer, Salem, Fordy, Kermi3, et. al. -, I do agree that the urgency is, in fact, yours, not ours.

    You've got C code intermixed with C++ code. (Yes, you do!.)

    Your code can't be duplicated because of the #include "stdafx.h" header.

    >Mabe some of you guys could fix some of my code so it's proper and may even work.
    C'mon.

    Decide which language you intend to program in. (Go back to my earlier post.)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM