Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,571 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,103 people online right now. Registration is fast and FREE... Join Now!




Infile/Linked Lists Issue

2 Pages V  1 2 >  
Reply to this topicStart new topic

Infile/Linked Lists Issue, Not sure where the problem lies

Vextor
post 16 Nov, 2005 - 08:13 PM
Post #1


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Alrighty. I'm gonna be on here alot until I get the hang of everything again. Below is a good portion of what I have so far for my log in portion of the program. This section is supposed to pull a user name (or list there of) and related password from a simple .dat file, insert the information into it's own node, if there's multiple nodes link them together, then get a user name and password from the user and compare them.

I compile the program perfectly but when I go to run it it crashes instantly on it. I'm not allocating memory right somewhere, somehow.

CODE

void UserDatabaseLoad()
{    
    User *NewUserNode;


    ifstream infile;
    infile.open("UserDatabase.dat", ios::in);
    if(infile)
    {
    
 while(!infile.eof())
 {
    
   NewUserNode = new User;
   

   if(NewUserNode != NULL)
   {
   infile.get(NewUserNode->UserName, 30);
   infile.ignore('\n');
   infile.get(NewUserNode->UserPass, 30);
   infile.ignore('\n');
   
   CreateNode(NewUserNode);
   }
   else
       cout<<"Error Loading UserDatabase.dat"<<endl;
 }
 infile.close();
 SysLogon();

    }
    else
    cout<<"Unable to load Userdatabase.doc"<<endl;
}

void CreateNode(User *NewUserNode) // Creates and inserts node into Linked Lists
{
    User *Hold;
    NewUserNode->UserNext = NULL;
    NewUserNode->UserPrev = NULL;

    if(UserHead == NULL) // If the list is empty
    {
 UserHead = NewUserNode; // creates a new node and assigns it to head
    }
    else // if list is NOT empty
    {
 UserHead = UserCurrent; //current node is now head node
 UserCurrent = NewUserNode; //head node takes the value of the new node

 UserHead->UserPrev = UserCurrent; // assigns the pointer to the previous node to the 'current node'
 UserCurrent->UserNext = UserHead; // current node's next pointer is assigned to the newly created head node
 
 if (UserCurrent->UserPrev == NULL) // if there is only 2 items in the list they are also linked together
 {
     UserHead->UserNext = UserCurrent;
     UserCurrent->UserPrev = UserHead;
 }

 else // if there is more then two in the list
 {
     Hold = UserCurrent;
     while(UserCurrent->UserPrev != NULL) // cycles through list until the end is reached
     {
   UserCurrent = UserCurrent->UserPrev;
     }

     UserHead->UserNext = UserCurrent; // assigns the end of the list node's previous pointer to the head pointer
     UserCurrent->UserPrev = UserHead; // links the head pointer to the end of the list
     UserCurrent = Hold; // reassigns the current Node

 }
 
    }

}

void SysLogon()
{
    char TempUserName[30];    
    char TempUserPass[30];
    int a;

    system("cls");
    
    UserCurrent=UserHead; // assigns the head node to current node.

    cout<<"Welcome to Vextor Banking Solutions' Banking System V. 1.00 (Test) (c) 2004 Vextor Inc. TM B-diddy Industries Rio Rancho NM\n";
    cout<<"Username:";
    cin.get(TempUserName, 30);
    cin.ignore(80, '\n');
    cout<<"\nEnter Password\nPassword: "<<endl;
    cin.get(TempUserPass, 30);
    cin.ignore(80, '\n');

    for(a=1; a<=UserCurrent->UserNodeCount; a++) //creates a loop based off number of nodes
    {
 if(strcmp(UserCurrent->UserName, TempUserName) == 0) // compares current node's username info to user entered info
 {
     
     if(strcmp(UserCurrent->UserPass, TempUserPass) == 0) // compares passwords
     {
   cout<<"Welcome to Vextor Banking\n"<<endl;
       MainMenu();
     }
     else
     {
   cout<<"System Login Failed: Invalid Password\n"<<endl;
   SysLogon();
     }
 }
 else
 {
 UserCurrent=UserCurrent->UserPrev; // cycles to next node to verify information again
 }
    }
    cout<<"No such User found\n";
    
}


Oh, and any suggestions on making the program more efficient would be greatly appreciated as well.
User is offlineProfile CardPM

Go to the top of the page


Amadeus
post 17 Nov, 2005 - 08:14 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


Vextor, my apologies, I'm not currently near a compiler, so cannot do this myself.

Can you insert pauses (wait for char, any sort of user input) before each line of executable code, so see which ones execute before the crash (and output each loop iteration as well)?
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 17 Nov, 2005 - 08:22 AM
Post #3


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Yeah for sure. Currently i'm at work so I'll have to wait until this evening but i'll do that and check it out myself. If I can't figure it out then i'll post what happens smile.gif Thanks though. Until I can find a company that will allow me to download and install a compiler im stuck doing in my off time mad.gif

This post has been edited by Vextor: 17 Nov, 2005 - 08:25 AM
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 17 Nov, 2005 - 01:06 PM
Post #4


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


From the code you've showen us:
UserHead is undeclared/undefined
UserCurrent is undeclared/undefined

CODE

UserHead = UserCurrent; //Not sure whta UserCurrent is, but why make it the head?
UserCurrent = NewUserNode; //Did you mean for UserCurrent to be Tail? Point to the last node in the list?

UserHead->UserPrev = UserCurrent;  //There is nothing before the head, head comes first therefore Head->Previous = NULL
UserCurrent->UserNext = UserHead; //This would make the UserCurrent point to the head


after that i really kind of lose track of what you are trying to do.


Not too long ago i created a LinkedList class, it was pretty good, for storying data, maybe you wanna give it a try?

I think its pretty easy to use, and very convinient. I dont have any comments on it, so if you need clarification on something just IM me or post here

My AIM sn is StupidTerorrists


Attached File(s)
Attached File  linkedlist.h ( 1.97k ) Number of downloads: 19
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 17 Nov, 2005 - 02:17 PM
Post #5


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


UserHead and UserCurrent are pointers to the User Class. I just didn't post that part.

Basically i'm just adding the node to the head of the list and linking each node to the one infront of it and the one in back.

After looking at your class I have noticed a few things.

- I don't believe i'm using classes as efficiently or correctly as I can.
- I need to read up on Templates crazy.gif
- and I will likely NOT use yours smile.gif

The reason is because I'm doing this project to learn. The best way I learn is by doing. I'll use yours as a reference (and I thank you profoundly) but I need to do this myself.

But after comparing the two (yours and mine) the only difference I see is that you add the nodes to the end whereas I'm attempting to add them to the head.

thank you again though.
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 17 Nov, 2005 - 02:32 PM
Post #6


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


Very welcome!

You are trying to add the nodes to the head? The head can only point to 1 node, unless the head class has more than one pointers to nodes.

But in a generic linked list, Head is usually the first element in the linked list, the tail is the last, and you have a current which is used to itirate through the list. Everything else is in order from first added to last added.

If i was you in the function CreateNode what i would do is check if the head is empty, if so asign the head to point to NewUserNode, else asign the last node's next pointer to the NewUserNode. Also set NewUserNode previous pointer to point to the Node before it, or the tail at the time. Then set the tail to point to NewuserNode.


I like how you have a pointer in the Node class that points to the previous Node. That creates a linked list that can be itirated both ways, in my Linked List you can only go forward, if you wanna go back you have to start from begining and itirate to the one you need.
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 17 Nov, 2005 - 02:44 PM
Post #7


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


I see what you're saying. What i'm attempting to do is a doublly/circularly linked list - I suppose I should have mentioned that! biggrin.gif

My HeadPtr would be the 'first' in the list wink2.gif

I'm reading on templates now - I'll just have to make sense of it.

With that I think I can clean up the program a bit and create a .h file to handle most of this. I just got done reading another tutorial on OOP and it gave me some good ideas.

In any case i'm going to check it out a bit more closer when I get home - which will be in about 2 hours blink.gif I'll post what I found out/change.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Nov, 2005 - 07:46 AM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


Vextor, I'm sure you're coming along well, but I just wanted ti let you know that you do not need templates for a circular, doubly linked list...or any linked list, for that matter.

And congrats on going for that oen...a circular double linked list will teach you all you need to know about linked lists!
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 18 Nov, 2005 - 08:06 AM
Post #9


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Well after spending a bit of time on it last night and inserting a few well placed pauses I finally got it to work! Woohoo! That also includes the verification.

As of now it loads the info, creates lists and compares the info on the lists successfully.

But I managed to get the pointers mixed up somewhere so it's not pointing to EVERY pointer. That is however something I believe I can handle. I'll post everything once I get done.


As for templates - I know I don't NEED them but i'm under the impression they tend to make things easier... right?

I've read two tutorials on them and I still don't understand. Can somone just give me a quick run down on thier implementation?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Nov, 2005 - 08:15 AM
Post #10


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


Templates do make things easier...they tend to remove the need for duplication of code/classes.

Have you taken a look at this one?

This book is one of the best, but is not free...the code examples are, however.
User is offlineProfile CardPM

Go to the top of the page

Vextor
post 18 Nov, 2005 - 10:00 AM
Post #11


D.I.C Regular

Group Icon
Joined: 22 May, 2002
Posts: 288



Thanked 1 times

Dream Kudos: 25
My Contributions


Thanks - I just got done looking through it. Unfortunately just reading it isn't enough I think. I'm still at a loss, I'll have to just sit down some time this weekend and play around with it. I'll definately use these resources though, thanks again!

one quesiton.

If i were to use mrafcho001's template class above would I be able to use that same code for say... a customer set of nodes with information like account numbers and personal info, and a User set of nodes with user info like Username and Password? Is that how it works?

oh and what do the ? and single : operators do? as in...

CODE

size = s > 0 && s < 1000 ? s : 10;


pulled right out of the resource you gave me.

Thanks again - you've been an great help.

This post has been edited by Vextor: 18 Nov, 2005 - 10:02 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 18 Nov, 2005 - 11:28 AM
Post #12


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,158



Thanked 32 times

Dream Kudos: 25
My Contributions


Those comprise a tertiary operator...a sort of IF statement. The following
CODE

return a > b ? a : b;

translates as
CODE

if(a>b)
  return a;
else
  return b;

Essentially, the tertiary operator says 'if the condition is met, then a, if it is not, then b'
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/20/08 03:17AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month