Click here to Skip to main content
15,921,212 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Question about CEdit and CSpinButtonCtrl Pin
Uwe Keim29-Nov-04 4:10
sitebuilderUwe Keim29-Nov-04 4:10 
GeneralRe: Question about CEdit and CSpinButtonCtrl Pin
Uwe Keim29-Dec-04 18:58
sitebuilderUwe Keim29-Dec-04 18:58 
GeneralMFC help for a beginner Pin
Member 153369527-Nov-04 15:45
Member 153369527-Nov-04 15:45 
GeneralRe: MFC help for a beginner Pin
John R. Shaw27-Nov-04 18:22
John R. Shaw27-Nov-04 18:22 
GeneralRe: MFC help for a beginner Pin
rasha200327-Nov-04 21:41
rasha200327-Nov-04 21:41 
GeneralRe: MFC help for a beginner Pin
Member 153369528-Nov-04 4:08
Member 153369528-Nov-04 4:08 
GeneralRe: MFC help for a beginner Pin
bitpusher28-Nov-04 7:46
bitpusher28-Nov-04 7:46 
GeneralProblem with derivation Pin
Cramp27-Nov-04 15:05
Cramp27-Nov-04 15:05 
First of all, I want to thanks that you spend your time to read my problem.

I have three classes in my program. Student is the base class. Graduate is derivated from Student class. studentDB is a class that includes both Student and Graduate class.

I use a STL container to save the data which could be Student or Graduate .

When I save data to an outfile, I got a problem. Visual C++ shows me a message below.

'show_thesis_topic' : is not the member of 'student'.

I know I can fix the problem if I create a Graduate class instead of Student class.

But I just want to know have any idea that I can also use to fix the problem.

Thanks your help. Bless you.

//Student.h
#ifndef __STUDENT
#define __STUDENT
class student //I define regular a student information here.
{
public:
void set_lastname(std::string );
void set_firstname(std::string );
void set_ssn(std::string );
void set_age(int);
void set_phone(std::string );
std::string show_lastname();
std::string show_firstname();
std::string show_ssn();
int show_age();
std::string show_phone();
std::string show_type_of_student();
void set_type_of_student(std::string);
virtual void display();
private:
std::string lastname;
std::string firstname;
std::string ssn;
int age;
std::string phone;
std::string type_of_student;
};

#endif


//student.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "student.h"
using namespace std;
//--------------------------------------------------------------------------------
// The following functions declare for the class of student.
//--------------------------------------------------------------------------------
void student::display() //the function will printout the student information out to screen.
{
cout << "\tStudent Data:\n";
cout << "\t\tSSN: " << ssn << endl;
cout << "\t\tLast Name: " << lastname << endl;
cout << "\t\tFirst Name: " << firstname << endl;
cout << "\t\tPhone Number: " << phone << endl;
cout << "\t\tAge: " << age << endl;
}
std::string student::show_lastname()
{
return lastname;
}
std::string student::show_firstname()
{
return firstname;
}
std::string student::show_ssn()
{
return ssn;
}
int student::show_age()
{
return age;
}
std::string student::show_phone()
{
return phone;
}
std::string student::show_type_of_student()
{
return type_of_student;
}
void student::set_lastname(std::string text)
{
lastname=text;
}
void student::set_firstname(std::string text)
{
firstname=text;
}
void student::set_ssn(std::string text)
{
ssn=text;
}
void student::set_age(int number)
{
age=number;
}
void student::set_phone(std::string text)
{
phone=text;
}
void student::set_type_of_student(string text)
{
type_of_student=text;
}

//Graduate.h
#include "student.h"
#ifndef __GRADUATE
#define __GRADUATE

class Graduate: public student // I define extra information for a graudate student
{
public:
std::string show_thesis_topic();
void set_thesis_topic(std::string);
std::string show_adviser();
void set_adviser(std::string);
void display();
private:
std::string thesis_topic;
std::string adviser;
};
#endif

//Graduate.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "Graduate.h"
using namespace std;
void Graduate::display()
{
cout << "\tGraduate Data:\n";
cout << "\t\tSSN: " << student::show_ssn() << endl;
cout << "\t\tLast Name: " << student::show_lastname() << endl;
cout << "\t\tFirst Name: " << student::show_firstname() << endl;
cout << "\t\tPhone Number: " << student::show_phone() << endl;
cout << "\t\tAge: " << student::show_age() << endl;
cout << "\t\tThesis: " << thesis_topic << endl;
cout << "\t\tAdvisor: " << adviser << endl;
}
void Graduate::set_adviser(string text)
{
adviser=text;
}
void Graduate::set_thesis_topic(string text)
{
thesis_topic=text;
}
string Graduate::show_adviser()
{
return adviser;
}
string Graduate::show_thesis_topic()
{
return thesis_topic;
}
//StudentDB.h
#include "student.h"
#include "Graduate.h"
#include <list>
#ifndef __STUDENTDB
#define __STUDENTDB
class student_db
{
public:
// add a regular student to the database – return true if successful, false otherwise
bool add_Student(std::string , std::string , std::string , int , std::string);
// add a graduate student to the database – return true if successful, false otherwise
bool add_Graduate(std::string , std::string , std::string , int , std::string , std::string , std::string );
// delete student with a particular social security number – return true if successful
// and false otherwise
bool delete_Student(std::string);
// display the data for student if present in the database – otherwise error message
void display_Student(std::string);
// display data for ALL the student in the database
void display_all();
// read data from disk - return true if data exist, false otherwise.
bool read_data(std::string,std::string);
// The function will write out a table of students.
void write_result();
// The function will return a value that showing how many records in database.
int how_many_record();
student_db();
~student_db();
private:
// The function will return true if find student in list, false otherwise
bool searchSSN(std::string);
std::ifstream Openfile;
std::ofstream Savefile;
std::string input_filename; //input_filename record the inputfile name.
std::string output_filename; //output_filename record the outfilename;
int how_many_data;
std::list<student *> database;
};
#endif

//StudentDB.cpp
#include <iostream>
#include <string>
#include <list>
#include <fstream>
#include <iomanip>
#include "studentDB.h"
using namespace std;
// The function will return a value that showing how many records in database.
int student_db::how_many_record()
{
return how_many_data;
}
// add a regular student to the database – return true if successful, false otherwise
bool student_db::add_Student(string ssn, string lastname, string firstname, int age, string phone)
{
if(!searchSSN(ssn)) //if the SSN cann't find in the array, saving a new record to the array.
{
student *temp_student=new student();
temp_student->set_type_of_student("Student");
temp_student->set_ssn(ssn);
temp_student->set_lastname(lastname);
temp_student->set_firstname(firstname);
temp_student->set_age(age);
temp_student->set_phone(phone);
database.push_front(temp_student);
return true;
}
else
return false;
}
// add a graduate student to the database – return true if successful, false otherwise
bool student_db::add_Graduate (string ss, string last, string first, int age, string ph, string th, string ad)
{
if(!searchSSN(ss)) //if the SSN cann't find in the array, saving a new record to the array.
{
Graduate *temp_student=new Graduate();
temp_student->set_type_of_student("Graduate");
temp_student->set_ssn(ss);
temp_student->set_lastname(last);
temp_student->set_firstname(first);
temp_student->set_age(age);
temp_student->set_phone(ph);
temp_student->set_adviser(ad);
temp_student->set_thesis_topic(th);
database.push_front(temp_student);
return true;
}
else
return false;
}
// delete student with a particular social security number – return true if successful
// and false otherwise
bool student_db::delete_Student(string ssn)
{
bool is_found=false;
list<student *>::iterator database_list;
database_list=database.begin();
if(searchSSN(ssn))
{
while (!is_found && database_list != database.end())
{
if ((*database_list)->show_ssn()==ssn)
is_found = true;
else
database_list++;
}
}
if(is_found) //if the index number is not -1, the program will start remove a record.
{
delete *database_list; // return storage for the car to heap
database.erase(database_list);
return true;
}
else
return false;
}
// display the data for student if present in the database – otherwise error message
void student_db::display_Student(string ssn)
{
if(searchSSN(ssn))
{
bool is_found=false;
list<student *>::iterator database_list;
database_list=database.begin();
while (!is_found && database_list != database.end())
{
if ((*database_list)->show_ssn()==ssn)
is_found = true;
else
database_list++;
}
if(is_found)
{
(*database_list)->display();
}
}
else
cout << "Student with ssn: " << ssn << " NOT found\n";
}
// display data for ALL the student in the database (using dynamic binding)
void student_db::display_all()
{
list<student *>::iterator database_list;
database_list=database.begin();
{
while(database_list!=database.end())
{
if((*database_list)->show_type_of_student()!="Graduate")
cout << setw(10) << (*database_list)->show_lastname() << setw(10) << (*database_list)->show_firstname() << setw(16) << (*database_list)->show_ssn() << setw(18) << (*database_list)->show_phone() << setw(7) << (*database_list)->show_age() << endl;
else
//-----------------------------------------------------------------
// This is the place where I got the problem.
//
//-----------------------------------------------------------------
cout << setw(10) << (*database_list)->show_lastname() << setw(10) << (*database_list)->show_firstname() << setw(16) << (*database_list)->show_ssn() << setw(18) << (*database_list)->show_phone() << setw(7) << (*database_list)->show_age() << setw(7) << (*database_list)->show_thesis_topic << setw(7) << (*database_list)->show_advise() << endl;
database_list++;
}
}
cout << endl;
}
student_db::student_db()
{
how_many_data=0;
}
student_db::~student_db()
{
Savefile.close();
Openfile.close();
}
// The function will write out a table of students
void student_db::write_result()
{
/*
list<student *>::iterator database_list;
database_list=database.begin();
{
while(database_list!=database.end())
{
if((*database_list)->show_type_of_student!="Graduate")
Savefile << setw(10) << (*database_list)->show_lastname() << setw(10) << (*database_list)->show_firstname() << setw(16) << (*database_list)->show_ssn() << setw(18) << (*database_list)->show_phone() << setw(7) << (*database_list)->show_age() << endl;
else
Savefile << setw(10) << (*database_list)->show_lastname() << setw(10) << (*database_list)->show_firstname() << setw(16) << (*database_list)->show_ssn() << setw(18) << (*database_list)->show_phone() << setw(7) << (*database_list)->show_age() << setw(7) << (*database_list)->show_thesis_topic() << setw(7) << (*database_list)->show_advise() << endl;
database_list++;
}
}
Savefile << endl;
*/
}
// The function will return true if find student in list, false otherwise
bool student_db::searchSSN(string ssn)
{
bool is_found=false;
list<student *>::iterator database_list;
database_list=database.begin();
while (!is_found && database_list != database.end())
{
if ((*database_list)->show_ssn()==ssn)
is_found = true;
else
database_list++;
}
return is_found;
}
// read data from disk - return true if data exist, false otherwise.
bool student_db::read_data(string in_filename, string out_filename)
{
Openfile.open(in_filename.c_str());
if(Openfile==NULL) //IF FILE DOES NOT EXIST, finish the function.
{
return false;
}
else // executing this scope if the file exist
{
Savefile.open(out_filename.c_str());
input_filename=in_filename; //recording a name of the inputfile to filename[0]
output_filename=out_filename; //recording a name of the outputfile to filename[1]
string temp_use; //the variable will save a string that read from inputfile for temp.
int temp_int_use; //the variable will save a integer that read from inputfile for temp.
while(!Openfile.eof()) //leaving for loop if there is nothing in the file.
{
Openfile >> temp_use;
if(Openfile==NULL)
break;
if(temp_use!="Graduate") //if the data that reads from the input file is belong Graduate, creating a graduate object, otherwise a student object.
{
student *temp_student=new student(); //creating a new student class to save each record that read from an input file.
temp_student->set_type_of_student(temp_use);
Openfile >> temp_use;
temp_student->set_lastname(temp_use);
Openfile >> temp_use;
temp_student->set_firstname(temp_use);
Openfile >> temp_use;
temp_student->set_ssn(temp_use);
Openfile >> temp_use;
temp_student->set_phone(temp_use);
Openfile >> temp_int_use;
temp_student->set_age(temp_int_use);
database.push_front(temp_student);
how_many_data++;
}
else
{
Graduate *temp_student=new Graduate(); //creating a new student class to save each record that read from an input file.
temp_student->set_type_of_student(temp_use);
Openfile >> temp_use;
temp_student->set_lastname(temp_use);
Openfile >> temp_use;
temp_student->set_firstname(temp_use);
Openfile >> temp_use;
temp_student->set_ssn(temp_use);
Openfile >> temp_use;
temp_student->set_phone(temp_use);
Openfile >> temp_int_use;
temp_student->set_age(temp_int_use);
Openfile >> temp_use;
temp_student->set_thesis_topic(temp_use);
Openfile >> temp_use;
temp_student->set_adviser(temp_use);
database.push_front(temp_student);
how_many_data++;
}
}
return true;
}
}

//Main program
#include <iostream>
#include <fstream>
#include <string>
#include "studentDB.h"
using namespace std;
int main()
{
cout << "\tSTUDENT DATABASE PROGRAM\n";
string filename[2]; //declare an array to save a input filename and a output filename;
cout << "\tEnter input file name: ";
cin >> filename[0];
cout << "\tEnter output file name: ";
cin >> filename[1];
student_db database;
if(database.read_data(filename[0],filename[1]))
{
cout << "\tData for " << database.how_many_record() << " students was read into the database…\n";
bool should_I_leave=false;
char command; //The variable will save the command that user choose.
string temp_info[6];
int temp_age;
while(!should_I_leave) //When user type Q or q will leave loop.
{
cout << "Enter command (A -> add, R -> remove, D -> display, S -> show all, Q -> quit): ";
cin >> command;
switch (command)
{
case 'A': case 'a':
cout << "\t\tWhat type of Student (r/R regular; g/G graduate): "; // If the user chooses r, the program will call add_Student function. If the user chooses g, the program will call add_Graduate function.
cin >> command;
switch (command)
{
case 'R': case 'r':
cout << "\t\t\tEnter social security number: ";
cin >> temp_info[0];
cout << "\t\t\tEnter last name: ";
cin >> temp_info[1];
cout << "\t\t\tEnter first name: ";
cin >> temp_info[2];
cout << "\t\t\tEnter age: ";
cin >> temp_age;
cout << "\t\t\tEnter phone: ";
cin >> temp_info[3];
if(database.add_Student(temp_info[0],temp_info[1],temp_info[2],temp_age,temp_info[3]))
cout << "\t\tStudent added!\n";
else
cout << "\t\tStudent with ssn: " << temp_info[0] << " already exist!\n";
break;
case 'G': case 'g':
cout << "\t\t\tEnter social security number: ";
cin >> temp_info[0];
cout << "\t\t\tEnter last name: ";
cin >> temp_info[1];
cout << "\t\t\tEnter first name: ";
cin >> temp_info[2];
cout << "\t\t\tEnter age: ";
cin >> temp_age;
cout << "\t\t\tEnter phone: ";
cin >> temp_info[3];
cout << "\t\t\tEnter thesis: ";
cin >> temp_info[4];
cout << "\t\t\tEnter advisor: ";
cin >> temp_info[5];
if(database.add_Graduate(temp_info[0],temp_info[1],temp_info[2],temp_age,temp_info[3],temp_info[4],temp_info[5]))
cout << "\t\tStudent added!\n";
else
cout << "\t\tStudent with ssn: " << temp_info[0] << " already exist!\n";

break;
}
break;
case 'D': case 'd':
cout << "\tEnter ssn of student to display: ";
cin >> temp_info[0];
database.display_Student(temp_info[0]);
break;
case 'R': case 'r':
cout << "\tEnter ssn of student to delete: ";
cin >> temp_info[0];
if(database.delete_Student(temp_info[0]))
cout << "\t\tStudent deleted!\n";
else
cout << "Student with ssn: " << temp_info[0] << " NOT found!\n";
break;
case 'S': case 's':
database.display_all();
break;
case 'Q': case 'q':
cout <<"Leaving the main loop\n";
cout <<"\tWritten output to " << filename[1] << endl;
database.write_result();
should_I_leave=true;
break;
}
}
}
else
{
cout << "the name of the file does not exist\n";
}
cout << "Exiting database Program…\n";
return 0;
}

GeneralRe: Problem with derivation Pin
John R. Shaw27-Nov-04 18:09
John R. Shaw27-Nov-04 18:09 
Generalhooking Pin
gamitech27-Nov-04 11:15
gamitech27-Nov-04 11:15 
GeneralRe: hooking Pin
John R. Shaw27-Nov-04 17:26
John R. Shaw27-Nov-04 17:26 
GeneralRe: hooking Pin
Roger Allen29-Nov-04 2:34
Roger Allen29-Nov-04 2:34 
QuestionEz ? Pin
BaldwinMartin27-Nov-04 11:07
BaldwinMartin27-Nov-04 11:07 
AnswerRe: Ez ? Pin
John R. Shaw27-Nov-04 17:04
John R. Shaw27-Nov-04 17:04 
GeneralWinsock Problem/Question Pin
pshopaddict27-Nov-04 8:18
susspshopaddict27-Nov-04 8:18 
GeneralRe: Winsock Problem/Question Pin
John R. Shaw27-Nov-04 18:30
John R. Shaw27-Nov-04 18:30 
GeneralRe: Winsock Problem/Question Pin
pshopaddict27-Nov-04 20:10
susspshopaddict27-Nov-04 20:10 
GeneralRe: Winsock Problem/Question Pin
Anders Molin28-Nov-04 6:22
professionalAnders Molin28-Nov-04 6:22 
GeneralRe: Winsock Problem/Question Pin
John R. Shaw28-Nov-04 8:34
John R. Shaw28-Nov-04 8:34 
GeneralRe: Winsock Problem/Question Pin
pshopaddict29-Nov-04 5:21
susspshopaddict29-Nov-04 5:21 
Questionhow to run .exe with .dll Pin
Member 154670927-Nov-04 6:58
Member 154670927-Nov-04 6:58 
AnswerRe: how to run .exe with .dll Pin
gamitech27-Nov-04 11:02
gamitech27-Nov-04 11:02 
QuestionMDI VC++, How to pass parameters to view? Pin
wwwrabbit127-Nov-04 6:08
susswwwrabbit127-Nov-04 6:08 
AnswerRe: MDI VC++, How to pass parameters to view? Pin
John R. Shaw27-Nov-04 6:55
John R. Shaw27-Nov-04 6:55 
GeneralRe: MDI VC++, How to pass parameters to view? Pin
david_gilmour27-Nov-04 13:05
david_gilmour27-Nov-04 13:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.