Retrieving object members from pointer vectors

Hey guys, I'm working on an assignment, in which I read strings from text files, and based on these strings create objects. The strings are stored in the format objecttype;username;password;ID;name, where ';' is the delimiter and the objecttype is an integer that determines whether to create an Admin, Student or Tutor object. I don't have problems reading in the data, however when I get to GUI::login(), where the input username and password is supposed to be compared with the username and password stored in the userlist vector, the program hangs. The problems occurs at around line 84. What am I doing wrong? Your feedback is appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

enum Usertype {admin,student,tutor,generic};

class System {

         public:vector<User*> userlist;
		System();
		User* getUser(int i){return userlist[i];}    
	        void setUserlist(vector<User*> newlist){userlist = newlist;}
};

System::System()
{
   ifstream file;
   string obj;
   string filename("userinfo.txt");
   file.open(filename.c_str());
	
   if(!file)
	 cout<<"Error in opening user info file"<<endl;
	  
   else
   {
     string s[4];
	 char* copy;
	 User* ptr;
	 while(getline(file,obj))
	 {
	     copy = (char*) malloc(obj.length());
         strcpy(copy, obj.c_str());	  	   	   	     
		 int checktype = atoi(strtok(copy,";"));
		 s[0]= strtok(NULL,";");
		 s[1]= strtok(NULL,";");
		 s[2]= strtok(NULL,";");
		 s[3]= strtok(NULL,";");
			 
		 switch(checktype)
		 {
		    case admin: {Admin obj (s[0],s[1],s[2],s[3]);
			             ptr = &obj;
			            break;}
			
			case student: {Student obj (s[0],s[1],s[2],s[3]);
			               ptr = &obj;
			               break;}
			
			case tutor: {Tutor obj (s[0],s[1],s[2],s[3]);
			             ptr = &obj;
			             break;}
		 }
		 userlist.push_back(ptr);
      }
      delete copy;
}
}

class GUI
{
     System sys;
	 User* curr;
     public:  void login();
	          void menu();
};

void GUI::login()
{
    string username, password;
    int count = 0;
    bool success = false;
	
	do
	{
	  cout<<"Username: ";
	  getline(cin, username);
          cout<<"Password: ";
	  getline(cin, password);

	  for(int i=0;i<sys.userlist.size();i++)
	  {	   	    
                if(username==sys.userlist[i]->getUsername() && password==sys.userlist[i]->getPassword())
		 {
	           success = true;
		   curr = sys.userlist[i];
		 }
	  }
	   
	  if(!success)
	  {
	   count++;
	   cout<<(count<3?"Invalid username and/or password, please try again.":"Invalid username and/or password, system locked")<<endl;
	  }
	  
	  else
	    menu();

	 }while(count<3&&!success);
}

void GUI::menu()
{cout<<"Menu: "<<endl;}

int main() 
{   
	GUI driver;
	driver.login();	  
        return 0;
}
Last edited on
1
2
3
4
5
6
{
   Admin obj (s[0],s[1],s[2],s[3]); //local variable
   ptr = &obj;
}//dies here
//...
userlist.push_back(ptr);
Your pointer points to invalid memory.
You need to allocate the objects dynamically.

Also, line 33 is leaking memory.
You should `free()' what you `malloc()'
You should `delete' what you `new'

Please learn to indent.
How silly of me. The problem has been solved. Thanks for the help! What's wrong with my indentation though?
Topic archived. No new replies allowed.