Okay for an assigment I have to create a date book using a linked list, but I am having trouble getting the thing to link and display. My display loop is infinite for some reason and data entry fills my entire list with that one node's data. I am also supposed to be able to delete, save to a file, restore from finle and clear the list. However, the list doesn't work. Someone give me a hand please.

>> My display loop is infinite for some reason
Your loop runs while temp != 0, but you never change temp inside the loop. You have the same problem with while(i==0) in main.

Also note that you never actually allocate space for your nodes. You need to use new to create a node, then set its data and add it to the list.

Finally, you should be using <iostream> and <string>, not <iostream.h> and <string.h>, and your main function should be defined as int main().

11-15-2006

projektaquarius

object *event;
object *temp;
event=new object;

I allocate memory in the variable declarations. And the rest of what you posted was just style issues that didn't help at all

11-15-2006

projektaquarius

the damned assignment guidelines

Figured I should post these before people start posting anything else

With this assignment you will learn how to manage lists of data. You will use a linked list to organize the data.

The assignment is to write a menu driven program that manages a date book.

The Menu commands will be:

A....Add a new Entry

D....Delete an Entry

P....Display the date book on the screen in date/time order.

S....Save the date book to a file. //copies the entire date book contents to a file

R....Restore a date book from a file. //loads additional data from a file

C....Clear the date book. //gets rid of the entries

Q...Quit

You must provide an input file for your program containing 6 entries.
Whenever the user tries to save the date book, the program will output the current phonebook to a file in date order. You can overwrite any previous information in the file. The name of the output file will need to be prompted for.
You will use a struct or class for your date book entry and a linked list to organize the data in the program. Your entry will contain day, month, year, time and event information.
When you add an entry the program will ask the user for each of these fields on a separate line and dynamically allocate memory for storage of the information.
When you delete a field the program will ask you for the date and time of the record that should be deleted. The entry must be removed from the list and reallocated to the heap.
When you display the date book on the screen it must be in date/time order.
Clearing the date book will reallocate all of the memory for the entries to the heap.

Instructions:

Your input file must have 6 entries.
Your files should be called datebook.cpp and your input file should be called input.txt.
The datebook.cpp file must contain at least 8 user defined functions.
You must use function prototypes and don't use global variables.
Your code should be well designed, well commented and written with good style.

11-15-2006

Daved

>> I allocate memory in the variable declarations.
Yes, but you do that once. Your loop attempts to add a new entry over and over again. So you are just overwriting the previous entry.

>> the rest of what you posted was just style issues that didn't help at all
You must not have understood what I was saying.

The comments on the headers are more than just style. It is about what is correct and what is incorrect. You should change it to learn the proper way to code. Even if you don't care about that, some people copy and paste code you post into their own compilers to help you by testing it themselves. If your code is wrong and has compiler errors, then it takes extra effort to help you. Since you are the one requesting help, you should be the one to invest that extra effort, especially for something so trivial.

My first comment about while (temp != 0) is also not a style issue. It is your problem. That's why I put it first.

11-15-2006

projektaquarius

my bad I apologize about the temp loop thing (I didn't see what you were talking about). But I'm using an almost perfectly ansi correct compiler and it doesn't work without .h at the end of the library files, or with the int in front of main. Being sick and stressed is not a good combo. Sorry.

11-15-2006

manutd

What compiler are you using? int main is needed, and string.h is depracated. Any compiler that gives errors for int main isn't really a C++ compiler.

11-15-2006

Daved

No worries. When you are less stressed/sick then you might want to come back to it and figure out why the headers aren't working, but it is less important than the main problem right now.

11-15-2006

manutd

Sorry, didn't see sick/stressed. Sorry :(

11-15-2006

jafet

Quote:

But I'm using an almost perfectly ansi correct compiler

...you mean that Boreland [sic] fossil?

And this is a bit more of a style issue: system("pause") is nonstandard; it relies on the system having an appropriate PAUSE command, which it may not. You're better off with the standard, well-defined, portable cin.get().

You need to rethink your list construction and traversal logic. Try planning it out on a piece of paper first, then fit it into abstract pseudocode, before getting into the real deal.

Quote:

I allocate memory in the variable declarations.

But you leave a lot of garbage behind -- delete your variables after you're done with them. Since you're using structs already, you might as well go for a constructor-deconstructor pair. Right now your code is basically C masquerading as C++.

11-15-2006

projektaquarius

yeah I fixed that, the garbage is just remnants of stupid randomness I used to try and get it to work. but does cin.get work for strings or is it only arrays, I've only seen it used with an array. And the only reason I'm using a struct because I'm required to.

my ifs are not working, sorry guys, linked lists are not clicking with me

11-15-2006

Daved

You just posted code, do you have a question or problem?

I see you fixed the while (temp != 0) loop, but not the while (i == 0) loop. Your dynamic allocation looks ok from what I can see, although you should of course be adding code later to delete the memory.

Your insert function has a problem. If front is 0, you enter that first if statement and you make the newEvent the front. But then you don't return from the function so it continues on and tries to add the newEvent again. You will get a crash there. Return from the function or use else or else if so that the code will only go through one of those blocks of code.

11-15-2006

projektaquarius

I know this is probably annoying you but, it's due in 11 hours and i have to work tomorrow morning.