c++*Urgent*(full code)

What is Happening...->the output LastName = ,comes out to be the string in Trade field... getline and cin problem??
before Enter Name commd, there is a cin>> cmmd in program
Code:


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
#include <vector>
#include <iostream>
#include <string.h>
#include<fstream>
#include<limits.h>
using namespace std;

class employee
{
      public:
      employee(string name,string last,int sal)
      {mName =name;mLastName =last; mSalary= sal;}
     
     
       virtual string LastName()
       {
            return mLastName ;
            }   
                     
      virtual void printStats()
       {
       cout<<"Name ="<<mName<<endl<<"LastName ="<<mLastName<<endl<<"Salary ="<<mSalary<<endl;
     
       }
       virtual void save(ofstream& out)
       {
               out<<"Name ="<<mName<<endl;
               out<<"Last Name ="<<mLastName<<endl;
               out<<"Salary ="<<mSalary<<endl;
               
               }
                         
                   
      protected:
             string mName;
             string mLastName;
             int mSalary;
                 };

class manager :public employee
{
      public:
             manager(string name,string last,int sal,int meet,int vac)
             :employee(name,last,sal)
             {mMeet=meet; mVac =vac;}
            void printStats()
            {
                 employee::printStats();
                 cout<<"Meeting ="<<mMeet<<endl<<"Vacation ="<<mVac<<endl;
                 
                 }
                 string LastName()
                 {
                      employee::LastName();
                      }
                 
     
          void save(ofstream& out)
          {
               employee::save(out);
                  out<<"MANAGER:";
                   out<<"Meeting ="<<mMeet<<endl;
                  out<<"Vacation ="<<mVac<<endl;
                  out<<endl<<endl;
                  }       
                 
        protected:
                  int mMeet;
                  int mVac;
                  };

class engineer:public employee
{
      public:
             engineer(string name,string last,int sal,int numC,int exp,string trade)
             :employee(name,last,sal)
             {mNumC=numC; mExp =exp; mTrade =trade;}
             
             void printStats()
             {
                  employee::printStats();
                  cout<<"C++ members ="<<mNumC<<endl<<"Experience ="<<mExp<<endl<<"Trade ="<<mTrade<<endl;
                  }
                 
   
             
                  string LastName()
                 {
                      employee::LastName();
                      }
                   void save(ofstream& out)
                   {
                             employee::save(out);
                             out<<"ENGINEER:";
                           out<<"Members with c++"<<mNumC<<endl;
                           out<<"Experiance"<<mExp<<endl;
                           out<<"Trade ="<<mTrade<<endl;
                            out<<endl<<endl;
                           
                           }
                           
             protected:
                     int  mNumC;
                      int mExp;
                      string mTrade;
                      };

class researcher:public employee
{
    public:
           researcher( string name,string last,int sal,string school,string topic)
           :employee(name,last,sal)
           {mSchool =school; mTopic =topic;}
           
           void printStats()
           {
                employee::printStats();
                cout<<"School ="<<mSchool<<endl<<"Topic ="<<mTopic<<endl;
                }
               
        string LastName()
                 {
                      employee::LastName();
                      }
       
               
                 void save(ofstream& out)
                 {
                 employee::save(out);
                 out<<"RESEARCHER:";
                         out<<"School ="<<mSchool<<endl;
                         out<<"Topic ="<<mTopic<<endl;
                          out<<endl<<endl;
                         }
           protected:
                     string mSchool;
                     string mTopic;
                     };

int main()
{
   
   
    vector<employee*> database;
  int count =1;
 
ofstream out("~Spam~");

   employee* emp[50];
   
   
      string garbage;   
     
       
bool done =false;
int input=0;
int select1 =0;
int select2 =0;
int j=0;

string first; string last; int sal; int exp; int num; string trade1;string school;string topic;
int meet; int vac;
string LastName;

while(!done)
{
           
        cout<<"Database size="<<database.size()<<endl;
                          cout<<"Databse contains:" ;
                          if(database.size()==0)
                          {
                          cout<<"Null"<<endl;
                          cout<<endl<<endl;
                          }
                          else
                          {
                          for(int i=0;i<database.size();i++)
                          {
                                 
                                database[i]->printStats();
                                  cout<<endl<<endl;
                                  }
                                  }
                                           
           

cout<<"1.Add Employee 2.Remove Employee 3.Quit!";
cin>>select1;


switch(select1)
{
               case 1:
                 start:   cout<<"1.Engineer 2.Manager 3.Researcher";
                    cin>>select2;
                   cin.ignore();
                    switch(select2)
                    {
                          case 1:
                             
                           
                             
                              cout<<"Name ="; getline(cin,first);
                              cout<<endl;
                              cout<<"Last name="; getline(cin,last);
                              cout<<endl;
                              cout<<"Salary ="; cin>>sal;
                             
                              cout<<"Num c++ ="; cin>>num;
                              cin.ignore();
                              cout<<endl;
                               cout<<"Trade ="; getline(cin,trade1);
                              cout<<endl;
                              cout<<"EXPeriance ="; cin>>exp;
                              cin.ignore();
                              cout<<endl;
                             
                             
                             
                             
                                  emp[j] =  new engineer(first,last,sal,num,exp,trade1);                                                                                                                 
                              database.push_back( emp[j]);
                           
                              cout<<"Last Name = "<<emp[j]->LastName()<<endl;
                              cout<<endl;                             
                              j++;
                             
                              break;
                             
                              case 2:
                             .................Same..........
                              .................................       
                     case 2:

..........................
                        ...enter last name.. if LastName==emp[j]->LastName();.........database.erase( ...)...something like that
                              
                         
                         
                                                             
                         
                          break;
                         
                         
                          default:
                                  done =true;
                                  break;
                                  }
                                  }
                           
          
Last edited on
I'm not sure I'm understanding your problem description but try sticking with either 'getline(cin, XXX)' or 'cin >>> XXX'. Mixing them can result in unexpected behavior unless you're careful.
You know, bolding everything only makes your problem look more juvenile, more trivial and less relevant. It's like typing in all caps. Unless you actually intend to use it for emphasis it doesn't add to the situation. You can always just type normally.
Anyway, you need to post more code than that. You are making references to classes and variables that we can't see in that snippet. I'm also not sure I understand your problem fully.
Please read the parts with regards to indenting code properly.
http://cplusplus.com/forum/articles/16853/
Topic archived. No new replies allowed.