May 2, 2010 at 12:26am UTC
Our homework is to write to program one that will out put to a txt file and the other to read from a file. I can get my first program to write to file but I can not get my second file to read from the txt file. I will post them both here if anyone has any idea how to correct this issue that would be wonderful....
Here is my write file
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
class student
{
protected :
int id;
string fname;
string lname;
float gpa;
float hours;
float fees;
public :
student();
string getfname();
string getlname();
float getgpa();
float getfees();
float gethours();
int getid();
void setid(int );
void setgpa(float );
void setfees(float );
void sethours(float );
void setfname(string);
void setlname(string);
virtual void display()=0;
virtual void input()=0;
virtual void read_to_file(ifstream& filein)=0;
virtual void write_to_file(ofstream& fout)=0;
};
//*****BASE CLASS MEMBERS****
student::student()
{
setlname("" );
setfname("" );
}
int student::getid()
{return id;}
string student::getfname()
{return fname;}
string student::getlname()
{return lname;}
float student::getgpa()
{return gpa;}
float student::gethours()
{return hours;}
float student::getfees()
{return fees;}
void student::setid(int i)
{id=i;}
void student::setfname(string n)
{fname=n;}
void student::setlname(string l)
{lname=l;}
void student::setgpa(float g)
{gpa=g;}
void student::sethours(float h)
{hours=h;}
void student::setfees(float f)
{fees=f;}
//*****UDERGRAD CLASS******
class Undergrad: public student
{
private :
string lvl;
bool FA;
string housing;
public :
bool getFA();
string getlvl();
string gethousing();
void setlvl();
void sethousing();
void Undergrad::setFA(bool );
void Undergrad::input();
void Undergrad::display();
void read_to_file(ifstream& filein);
void write_to_file(ofstream& fout);
};
//*******UNDERGRAD MEMBERS*********
bool Undergrad::getFA()
{return FA;}
string Undergrad::getlvl()
{return lvl;}
string Undergrad::gethousing()
{return housing;}
void Undergrad::setlvl( )
{int selection;
cout << "Please Choose Student Year" << endl;
cout << "1.Freshman 2.Sophomore 3.Junior 4.Senior" << endl;
cin>>selection;
switch (selection)
{
case 1: lvl="Freshman" ;
break ;
case 2: lvl="Sophomore" ;
break ;
case 3: lvl="Junior" ;
break ;
case 4: lvl="Senior" ;
break ;
default : cout<<"Please enter a correct value" ;
}
}
void Undergrad::sethousing( )
{int selection;
cout << "Please Enter Housing Type:" << endl;
cout << "1.Dorm 2.Fraternity 3. Sorority" << endl;
cin>>selection;
switch (selection)
{
case 1: housing="Dorm" ;
break ;
case 2: housing="Fraternity" ;
break ;
case 3: housing="Sorority" ;
break ;
default : cout<<"Please enter a correct value" ;
}
}
void Undergrad::setFA(bool )
{int tempFA;
cout << "Does student have financial aid?" << endl;
cout << "Please Enter: 1 for Yes or 0 for No.\n" <<endl;
cin>>tempFA;
if (tempFA==1)
{FA=true ;}
if (tempFA==0)
{FA=false ;}
}
void Undergrad::input()
{string fname, lname, housing, lvl; int id; float gpa, hours, fees; int FA;
cout<<"Enter first and last name: " ;
cin>>fname>>lname;
setfname(fname); setlname(lname);
cout<<"Enter id number:\n " ;
cin>>id;
setid(id);
cout<<"Enter GPA:\n " ;
cin>>gpa;
setgpa(gpa);
cout<<"Enter hours:\n " ;
cin>>hours;
sethours(hours);
sethousing();
setlvl();
setFA(FA);
Undergrad::display();
}
void Undergrad::display()
{float fees;
fees=125*gethours()+185;
cout<< "\nStudent Fees:\n" << fees;
}
class grad:public student
{
private : bool TA; // Teaching Assistend
string TT; //thesis topic
public :
void setTA(bool );
void setTT();
bool getTA();
string getTT();
void input();
void display();
void read_to_file(ifstream& filein);
void write_to_file(ofstream& fout);
};
bool grad::getTA()
{return TA;}
string grad::getTT()
{return TT;}
void grad::setTA(bool )
{
bool tempTA;
cout << "Is Student Teaching Assident?" << endl;
cout << "1.Yes 0.No" << endl;
cin >> tempTA;
if (tempTA==1)
{TA=true ;}
if (tempTA==0)
{TA=false ;}
}
void grad::setTT()
{
cout << "Please Enter Graduates Thesis Topic" << endl;
cin >> TT;
}
void grad::input()
{string fname, lname; int id; float gpa,hours,fees; int TA; string TT;
cout<<"Enter First and Last name: " ;
cin>>fname>>lname;
setfname(fname); setlname(lname);
cout<<"Enter ID number: " ;
cin>>id;
setid(id);
cout<<"Enter GPA: " ;
cin>>gpa;
setgpa(gpa);
cout<<"Enter hours: " ;
cin>>hours;
sethours(hours);
setTA(TA);
setTT();
grad::display();
}
void grad::display()
{float fees;
(fees=200*gethours()+275);
cout<< "\nStudent Fees:\n" << fees;
}
int main()
{
ifstream filein;
filein.open("Undergrad.txt" , ios::app);
filein.open("grad.txt" ,ios::app);
ofstream fout;
int Q=1;
int type;
student*sPtr;
unsigned long n=1;
do {
cout << "\n" ;
cout << "\nWould you like to create a New Student?" << endl;
cout <<"\nEnter 1. for Graduate and 2. for Undergrad:\n" << endl;
cout << "\nEnter 5 to Quit\n" ;
cin >> type;
if (type==1)
{sPtr=new grad;}
else if (type==2)
{sPtr=new Undergrad;}
else if (type==5)
{cout<<"Program Closing" ; n=0;}
else
{
cout << "\nComputer will now Self destruct\n" ;
system("pause" );
n=0;
}
{
if ((type==1)||(type==2))
{ sPtr -> input();
sPtr -> write_to_file(fout);
delete sPtr;
}
}
} while (n != 0);
filein.close();
getchar();
cin.clear();
return 0;
}
//****I/0****
void Undergrad::write_to_file(ofstream& fout)
{
fout.open("Undergrad.txt" , ios::app);
fout<< getfname()<< getlname()<< getid()<< getgpa() << getlvl()<< gethousing()<< getfees() << endl;
fout.close();
}
void grad::write_to_file(ofstream& fout)
{
fout.open("grad.txt" , ios::app);
fout << getfname()<< getlname()<< getid()<< gethours()<< getfees()<< getTA()<< getTT()<< endl;
fout.close();
}
void Undergrad::read_to_file(ifstream& filein)
{
string stType;
filein >> stType >> fname>> lname>> id >> gpa>> lvl>> hours >> housing >> fees;
/*cout<<" "<<fname()<<" "lname()<<" "<< id() << " "<<gpa << " " <<lvl <<" " << hours
<< " "<<housing<<" "<<fees<<endl;}*/ }
void grad::read_to_file(ifstream& filein)
{
string stType;
filein >> stType >> fname >> lname >>id >> gpa >> hours >> fees >> TA >> TT;
/*cout<<" "<< fname()<<" "<< lname()<<" " << id()<<" "<<gpa<<" "<< hours()<<" "<< fees()<<" " << getTA()
<<" "<< TT()<< endl;*/
}
Last edited on May 2, 2010 at 3:38am UTC
May 2, 2010 at 12:27am UTC
Here is my read file
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
int main()
{
ifstream filein;
filein.open("Undergrad.txt" , ios::in);
filein.open("grad.txt" ,ios::in);
ofstream fout;
student*sPtr;
int Q=1;
int type;
char a,b;
unsigned long n=1;
while (a>b);
{
cout << "\n" ;
cout << "\nPlease enter the file you would like to view:" << endl;
cout <<"\nEnter 1. for Graduate and 2. for Undergrad:\n" << endl;
cout << "\nEnter 5 to Quit\n" ;
cin >> type;
if (type==1)
{filein.open("grad.txt" ,ios::in);
while (filein.eof() == false )
{sPtr = new grad;
sPtr -> read_to_file(filein);
delete sPtr;
getchar();
filein.close();}}
else if (type==2)
{filein.open("Undergrad.txt" , ios::in);
while (filein.eof() == false )
{sPtr = new Undergrad;
sPtr -> read_to_file(filein);
delete sPtr;
getchar();
filein.close();}}
else if (type==5)
{cout<<"Program Closing" ; n=0;}
else
{
cout << "\nComputer will now Self destruct\n" ;
system("pause" );
n=0;
}
{
if ((type==1)||(type==2))
{ sPtr -> input();
sPtr -> read_to_file(filein);
delete sPtr;
}
}
} while (n != 0);
filein.close();
getchar();
cin.clear();
return 0;
}
Last edited on May 2, 2010 at 4:07am UTC
May 2, 2010 at 2:41am UTC
At the very least put your code inside [c ode]code here[/code] tags.
May 2, 2010 at 2:44am UTC
Once you put your code in [code] tags, I'll answer your question.
Nah, just kidding... but please put your code in [code] tags.
The functions you defined as virtual need to be not defined as virtual. Yes, they do, but they need not be redeclared in a derived class.
Also, earlier, within your declarations for your Undergrad class, you have a few functions with Undergrad:: stitched on inside the class block. Axe 'em!
-Albatross
Last edited on May 2, 2010 at 3:32am UTC
May 2, 2010 at 3:08am UTC
I see here that you output the data for grad students in the file like this:
fout << getfname()<< getlname()<< getid()<< gethours()<< getfees()<< getTA()<< getTT()<< endl;
This really isn't the way to go as you don't have something to separate the elements you output from one another... Try doing it like this:
1 2 3
fout << getfname() << endl << getlname() << endl;
fout << getid() << endl << gethours() << endl << getfees() << endl;
fout << getTA()<< endl << getTT()<< endl;
That is output each element in a different line. Now, I don't think it's hard to figure out a way to get them back...
Also the undergrad student file i/o needs to be fixed.
Last edited on May 2, 2010 at 3:10am UTC
May 2, 2010 at 3:23am UTC
ok I can see the fout problem now. But the virtual has to be defines that way its part of the assignment that is the only reason it is written like that... Thank I fixed some of the problems but still not working on the read file from txt...
Thank you I will keep working on it..
May 2, 2010 at 3:33am UTC
I edited my above post. The virtuality of those functions is fine, just don't redeclare a virtual function in your child class Undergrad.
-Albatross
May 2, 2010 at 3:45am UTC
Yes! It looks so much better!
filein.open("Undergrad" ,ios::in)
@Line 34
Are you sure that's what you wanted?
-Albatross
Last edited on May 2, 2010 at 3:45am UTC
May 2, 2010 at 3:52am UTC
This part of the file works
fout << getfname()<< getlname()<< getid()<< gethours()<< getfees()<< getTA()<< getTT()<< endl;
May 2, 2010 at 3:55am UTC
At this point I'm not sure what I want if you have any suggestion I'm all ears.
filein.open("Undergrad",ios::in)
May 2, 2010 at 3:57am UTC
LOL ok I see it need to be Undergrad.txt
May 2, 2010 at 3:57am UTC
I know. However, you're opening a file that shouldn't have anything in it. fout writes to "Undergrad.txt".
You saw.
-Albatross
Last edited on May 2, 2010 at 3:57am UTC
May 2, 2010 at 3:58am UTC
this is the error I am getting [Linker error] undefined reference to `vtable for student'
May 2, 2010 at 4:13am UTC
Wait a minute... I knew something was off, and it's so obvious...
How and when did you define the class "student" in your read file, along with all the child classes and functions?
-Albatross
Last edited on May 2, 2010 at 4:13am UTC
May 2, 2010 at 5:20am UTC
Ok I got all the problems with the writing file fixed now I just need to get the read file to read the txt..
May 2, 2010 at 5:49am UTC
Is there and easier way I can just write a small read txt file for just reading the txt? I know how to do it In C but not in C++..
May 2, 2010 at 5:55am UTC
Between the two of us, I prefer stdio.h over iostream for most operations. If you do too, use it. Anything you write in C will compile in C++.
-Albatross
May 2, 2010 at 6:21am UTC
It has to be C++ for my class :(