|
-
Oct 28th, 2002, 02:26 PM
#1
Thread Starter
Member
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 *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?
-
Oct 28th, 2002, 02:33 PM
#2
Monday Morning Lunatic
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
-
Oct 28th, 2002, 02:51 PM
#3
Thread Starter
Member
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.
-
Oct 28th, 2002, 03:01 PM
#4
Monday Morning Lunatic
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
-
Oct 28th, 2002, 03:12 PM
#5
Thread Starter
Member
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!
-
Oct 28th, 2002, 03:31 PM
#6
Monday Morning Lunatic
*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
-
Oct 28th, 2002, 05:09 PM
#7
Thread Starter
Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|