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* ¤t_list);
void Login(int i,Users* &head);
void Save(Users* &head);
//done
void NewUser(int i,Users* &head, Users* ¤t_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;
}