Need Linked list Help.

This is a discussion on Need Linked list Help. within the C++ Programming forums, part of the General Programming Boards category; I am having a small problem with my list. In order to get my save/load function to work in my ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    13

    Need Linked list Help.

    I am having a small problem with my list. In order to get my save/load function to work in my previous problem, I have decided to rewrite the list. However, when it displays the list in a seperate function, the last node in the list gets corrupted.
    ex.
    Trevor
    Rocko

    Polo
    Mano

    ||||||||||||||||||||||||||||||
    |||||||||||||||| --(these are part of the final node, the otehr 2 work fine)

    here is what I have so far.
    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <conFX2.h>
    #include <apstring.h>
    #include <apstring.cpp>
    #include <time.h>
    
    typedef struct Student
    {
    	int UserID;
    	char Fname[8];
    	char Sname[8];
    	int SepID;
    } astudent;
    
    struct Users
    {
    	Student user;
    	Users *next;
    }Userrecord;
    //userrecord->user.UserID
    //Userfile subject[25];
    //Userfile tsubject;
    
    //Function Prototypes
    void NewUser(int i,Users* &head, Users* &current_list);
    void Login(int i,Users* &head);
    void Save(Users* &head);
    
    //done
    
    
    
    void NewUser(int i,Users* &head, Users* &current_list)
    {
    	char cho;
    	int endloop=0;
    	//apstring Fname; 
    	//apstring Sname;
    	 //ID;
    	Users *list = new Users, *current_address, *previous_address;
    	current_list=list;
    	cout << "Welcome to the User creation Function"<<endl;
    	cout << "Please answer the following questions."<<endl;
    	for (;; list = list->next) 
    	{
    		//add previous addresses etc
    		cout <<"What is your first name? " <<endl;
    		//cout<<endl;
    		
    		cin.getline(list->user.Fname,8);
    		cout <<"\n";
    		//cout << list->user.Fname<<endl;
    		cout << "Now, your last name"<<endl;
    		cin.getline(list->user.Sname,8);
    		cout <<"\n";
    		cout << "Finally, enter your ID"<<endl;
    		list->user.UserID=rand();
    		cout <<"\n";
    		cout <<"Would you like to make a new User?"<<endl;
    		previous_address=current_address;
    		
    		cho=getch();
    	if (cho=='n')
    	{
    		//endloop=1;
    		list = previous_address->next = NULL;
    		delete current_address;
    		break;
    		
    	}
    	else
    	{
    		cout << " Preparing for new entry" <<endl;
    		getch();
    		current_address = list->next = new Users;
    		//list=list->next;
    	}
    	}//while(endloop!=1);
    	Save(current_list);
    	
    }
    void Save(Users* &head)
    {
    	
    	Users *savelist;
    	ofstream outfile;
    	//outfile.open("Profiles.dat",ios::out| ios::app |ios::binary);
    	//savelist=head;
    	int i=0;
    	for(savelist=head;savelist;savelist=savelist=savelist->next)
    	{
    		
    		cout << savelist->user.Fname<<endl;
    		cout << savelist->user.Sname<<endl;
    		//i++;
    		getch();
    		
    	}
    
    	
    
    }
    void Login(int i,Users* &head)
    {
    		
    }
    
    char menu()
    {
    	char ch;
    	clrscr();
    	gotoxy(15,5);
    
    	gotoxy(25,7);
    	cout <<"Would you like to...."<<endl;
    	gotoxy(25,8);
    	cout <<"[L]ogin"<<endl;
    	gotoxy(25,9);
    	cout<<"[C]reate a new user"<<endl;
    	gotoxy(25,10);
    	cout<<"[Q]uit?" <<endl;
    	ch = getch();
    	//ch=toupper(ch);
    	return ch;
    }
    void process(char ch,Users* &head)
    {
    //	Users* list;
    //	static Users* head;
    	static Users* current_list;
    	int i=0;
    	if(ch == 'c')
    		NewUser(i,head,current_list);
    	else if(ch == 'l') 
    		Login(i,head);
    	else if(ch == 's') 
    		Save(head);
    
    	
    }
    int main()
    {
    	char ch;
    	Users *head = NULL;
    	head = new(Users);
    	initscreen();
    	settitlebar("Tutorial");
    //	textcolor(fgWhite);
    //	textbackground(bgBlue);
    	clrscr();
    	ch = menu();
    	while (ch != 'Q')
    	{
    		process(ch,head);
    		ch = menu();
    	} 
    	uninitscreen();
    	return 0;
    }

  2. #2
    Unregistered
    Guest

    Post

    I don't know what is wrong with you list.. but using the STL will remove all your linked list problem (but in return you vill get STL problems... at first

    Code:
    #include <list>
    
    // creating a list of Students:
    std::list<Student*> StudentList;
    
    // new student
    
    Student user;
    //initialize user here, or whatever
    
    StudentList.push_back(&user);
    
    
    //loop the list
    
    std::list<Student*>::iterator listit;
    
    for(listit = STudentList.begin(); listit != StudentList.end; listit++){
        // do whatever you like to do here.... (*listit) will be your user.
        printf("%s %s",(*listit).Fname, (*listit).Sname);
    }

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    I'm not good enough to completely interpret your code, but

    Code:
    for(savelist=head;savelist;savelist=savelist=save
    list->next)
    If 'next' is NULL...?

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

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    13
    I found the problem. My entry code was deleting the last list oops....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 11:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21