C++ LLL!
Aug 22, 2013 at 3:52am UTC
Hi all!
I was just trying a simple c++ with LLL. I don't know why it adds data but when displaying, it shows nothing. Can anyone help? Thanks!
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
main.h //all the declaration of classes
#include<iostream>
using namespace std;
class profiledata
{
public :
profiledata();
~profiledata();
int addname(char newname[]);
int addssn(char newssn[]);
int adddob(char newdob[]);
int copy(profiledata &);
int display();
private :
char *name;
char *ssn;
char *dob;
};
struct node
{
profiledata nodedata;
node *next;
};
class operation
{
public :
operation();
~operation();
int add(profiledata &data);
int display();
private :
node *head;
};
implementation.cpp // implementation of all the member function
#include "main.h"
using namespace std;
profiledata::profiledata()
{
name = NULL;
ssn = NULL;
dob = NULL;
}
profiledata::~profiledata()
{
if (name)
{
delete name;
delete []name;
}
if (ssn)
{
delete ssn;
delete []ssn;
}
if (dob)
{
delete dob;
delete []dob;
}
}
int profiledata::addname(char newname[])
{
if (name)
delete []name;
name = new char [strlen(newname) + 1];
strcpy(newname,name);
return 1;
}
int profiledata::addssn(char newssn[])
{
if (ssn)
delete []ssn;
ssn = new char [strlen(newssn) + 1];
strcpy(newssn,ssn);
return 1;
}
int profiledata::adddob(char newdob[])
{
if (dob)
delete []dob;
dob = new char [strlen(newdob) + 1];
strcpy(newdob,dob);
return 1;
}
int profiledata::copy(profiledata &datas)
{
if (!name || !ssn || !dob)
return 0;
if (datas.name)
delete datas.name;
datas.name = new char [strlen(name) +1];
strcpy(datas.name,name);
if (datas.ssn)
delete datas.ssn;
datas.ssn = new char [strlen(ssn) +1];
strcpy(datas.ssn,ssn);
if (datas.dob)
delete datas.dob;
datas.dob = new char [strlen(dob) +1];
strcpy(datas.dob,dob);
return 1;
}
int profiledata::display()
{
if (!name)
return 0;
cout<<"NAME: " <<name<<endl;
cout<<"SSN: " <<ssn<<endl;
cout<<"DOB: " <<dob<<endl;
return 1;
}
operation::operation()
{
head = NULL;
}
operation::~operation()
{
node *temp = head;
if (head)
{
while (head != NULL)
{
temp = head;
delete []temp;
delete temp;
}
}
}
int operation::add(profiledata &data)
{
node *temp = head;
if (head == NULL)
{
head = new node;
data.copy(head->nodedata);
head ->next = NULL;
return 1;
}
if (head != NULL)
{
while (temp->next!=NULL)
{
temp = temp->next;
}
temp->next = new node;
temp = temp->next;
data.copy(temp->nodedata);
temp->next = NULL;
return 1;
}
}
int operation::display()
{
int count = 1;
node *temp = head;
if (!temp)
{
return 0;
}
else
{
while (temp != NULL)
{
cout<<endl<<count<<". " <<endl;
temp->nodedata.display();
temp = temp->next;
count++;
}
return 1;
}
}
application.cpp //user interaction
#include "main.h"
using namespace std;
int main()
{
char name[100];
char ssn[100];
char dob[100];
profiledata data1;
operation data;
int choice = 0;
do
{
cout<<endl<<endl<<"MAIN MENU:" <<endl<<endl;
cout<<"1. ADD PROFILE" <<endl;
cout<<"2. DISPLAY ALL" <<endl;
cout<<"Enter your choice: " ;
cin>>choice;
if (choice == 1)
{
cout<<endl<<"ENTER NAME: " ;
cin>>name;
cout<<endl<<"ENTER SSN: " ;
cin>>ssn;
cout<<endl<<"ENTER DOB: " ;
cin>>dob;
data1.addname(name);
data1.addssn(ssn);
data1.adddob(dob);
data.add(data1);
}
if (choice == 2)
{
data.display();
}
}while (choice>0&&choice<3);
}
Last edited on Aug 22, 2013 at 6:04am UTC
Aug 22, 2013 at 6:12am UTC
Your problem is to to much hard ,well,I am gonna do my best .
In your code you have those functions:
1 2 3
int addname(char newname[]);
int addssn(char newssn[]);
int adddob(char newdob[]);
right?
In those functions you have those strcpys!!
1 2 3
strcpy(newname,name);
strcpy(newssn,ssn);
strcpy(newdob,dob);
right?
shouldnt you turn them to
1 2 3
strcpy(name,newname);
strcpy(ssn,newssn);
strcpy(dob,newdob);
right?
uffffff that was not easy
Last edited on Aug 22, 2013 at 6:19am UTC
Aug 22, 2013 at 8:04am UTC
Aug 22, 2013 at 8:32am UTC
LoL :) . Thanks for the reply!
Aug 22, 2013 at 12:45pm UTC
By the way, stop shooting yourself on the foot
1 2
delete ssn;
delete []ssn;
is undefined behaviour.
If you
new
then
delete
If you
new []
then
delete []
You'll also need to implement a proper copy constructor and assignment operator.
However, you may simply use
std::string
and don't worry about memory management
Topic archived. No new replies allowed.