Click here to Skip to main content
15,900,258 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: School projects here is my code Pin
David Crow2-Nov-04 7:52
David Crow2-Nov-04 7:52 
GeneralRe: lab8dat.txt Pin
Izaquad2-Nov-04 9:10
Izaquad2-Nov-04 9:10 
GeneralRe: School projects Pin
includeh102-Nov-04 7:27
includeh102-Nov-04 7:27 
GeneralRe: Tips Pin
Budric B.2-Nov-04 7:59
Budric B.2-Nov-04 7:59 
GeneralFeature request - Limit the Editor Window size in Visual Studio NET Pin
van12-Nov-04 6:27
van12-Nov-04 6:27 
GeneralRe: Feature request - Limit the Editor Window size in Visual Studio NET Pin
BlackDice2-Nov-04 6:40
BlackDice2-Nov-04 6:40 
GeneralRe: Feature request - Limit the Editor Window size in Visual Studio NET Pin
Henry miller2-Nov-04 8:00
Henry miller2-Nov-04 8:00 
QuestionHow to define an array of the object in class ( with code ) Pin
Cramp2-Nov-04 6:19
Cramp2-Nov-04 6:19 
First. I appreciate that you spend your time to read my question.

I have question about how to define an object array at private area.

For example: I have a class which is call base_class; I want to create a new class which is called

new_class; But I want to define base_class[50] in my new class. Could someone help me?

When I am compiler my main program, I got following message.

"studentDB.h type name expected "

But I didn't get any wrong message when I was compiler both student.cpp and studentDB.cpp files.


The following code is what I write in my program;

//This is my main program--------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include "studentDB.h"
using namespace std;
int main()
{
string filename[2]; //declare an array to save a input filename and a output filename;
cout << "Enter input file name: ";
cin >> filename[0];
cout << "Enter output file name: ";
cin >> filename[1];
student_db database; //create a class
if(database.read_data(filename[0],filename[1]))
{
cout << "Data 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[4];
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 << "Enter social security number: ";
cin >> temp_info[0];
cout << "Enter last name: ";
cin >> temp_info[1];
cout << "Enter first name: ";
cin >> temp_info[2];
cout << "Enter age: ";
cin >> temp_age;
cout << "Enter phone: ";
cin >> temp_info[3];
if(database.add_Student(temp_info[0],temp_info[1],temp_info[2],temp_age,temp_info[3]))
cout << "Student added!\n";
else
cout << "Student with ssn: " << temp_info[0] << " already exist!\n";
break;
case 'D': case 'd':
cout << "Enter ssn of student to display: ";
cin >> temp_info[0];
database.display_Student(temp_info[0]);
break;
case 'R': case 'r':
cout << "Enter ssn of student to display: ";
cin >> temp_info[0];
if(database.delete_Student(temp_info[0]))
cout << "Student 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':
database.write_result();
should_I_leave=true;
break;
}
}
}
else
{
cout << "the name of the file does not exist\n";
}
return 0;
}


// I define the function of the student_db here.----------------------------------
//filename: studentDB.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include "student.h"
#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_in_database;
}
// add a 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)==-1) //if the SSN cann't find in the array, saving a new record to the array.
{
student_collect[how_many_in_database].set_ssn(ssn);
student_collect[how_many_in_database].set_lastname(lastname);
student_collect[how_many_in_database].set_firstname(firstname);
student_collect[how_many_in_database].set_age(age);
student_collect[how_many_in_database].set_phone(phone);
how_many_in_database++; //We add 1 to count after we add one person to the array.
SortBySSN();// The function will write out a table of students sorted by social security number (in ascending order)
return true;
}
else
return false;
}
//The function will call searchSSN function to search a data and searchSSN function will return a index.
// delete student with a particular social security number – return true if successful
// and false otherwise
bool student_db::delete_Student(string ssn)
{
int check=0;
check=searchSSN(ssn);
if(check!=-1) //if the index number is not -1, the program will start remove a record.
{
swap(check,how_many_in_database-1);
how_many_in_database--;
SortBySSN();
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)
{
int check=0;
check=searchSSN(ssn);
if(check!=-1)
{
cout << "\tStudent Data: \n";
cout << "\t\tSSN: " << student_collect[check].show_ssn() << endl;
cout << "\t\tLast Name: " << student_collect[check].show_lastname() << endl;
cout << "\t\tFirst Name: " << student_collect[check].show_firstname() << endl;
cout << "\t\tPhone Number: " << student_collect[check].show_phone() << endl;
cout << "\t\tAge: " << student_collect[check].show_age() << endl;
}
else
cout << "Student with ssn: " << ssn << " NOT found\n";
}
// display data for ALL the student in the database
void student_db::display_all()
{
cout << setw(10) << "Last Name" << setw(10) << "First Name" << setw(16) << "SSN" << setw(18) << "Phone Number" << setw(7) << "Age" << endl;
for(int i=0;i<how_many_in_database;i++)
{
cout << setw(10) << student_collect[i].show_lastname() << setw(10) << student_collect[i].show_firstname() << setw(16) << student_collect[i].show_ssn() << setw(18) << student_collect[i].show_phone() << setw(7) << student_collect[i].show_age() << endl;
}
cout << endl;

}
student_db::student_db()
{
max_people_in_database=50; //How many people do you have?
}
student_db::~student_db()
{
Savefile.close();
Openfile.close();
}
// The function will write out a table of students sorted by SSN or Last Name.
//The parameter that is called datanumber will tell the fuction how many loop does it need.
void student_db::write_result()
{
Savefile << endl;
for(int i=0;i<how_many_in_database;i++)
{
Savefile << setw(10) << student_collect[i].show_lastname() << setw(10) << student_collect[i].show_firstname() << setw(16) << student_collect[i].show_ssn() << setw(18) << student_collect[i].show_phone() << setw(7) << student_collect[i].show_age() << endl;
}
Savefile << endl;
}
//The function that use binary search algorithm will return -1 if it can not find same SSN in the array or return index if it find the SSN.
//The first parameter tell the function SSN that the function need to check.
//The second parameter tell the function how many loop it should do.
int student_db::searchSSN(string ssn)
{
int returnvalue=0;
if(ssn==student_collect[how_many_in_database/2-1].show_ssn())
{
returnvalue=how_many_in_database/2-1;
}
else if( ssn > student_collect[how_many_in_database/2-1].show_ssn())
{
for(int i=how_many_in_database/2; i<how_many_in_database;i++)
{
if(ssn==student_collect[i].show_ssn())
{
returnvalue=i;
i=how_many_in_database; //finding the value, leaving loop
}
else
returnvalue=-1;
}
}
else
{
for( int i=0;i < how_many_in_database/2 ; i++)
{
if(ssn==student_collect[i].show_ssn())
{
returnvalue=i;
i=how_many_in_database; //finding the value, leaving loop
}
else
returnvalue=-1;
}
}
return returnvalue;
}
// The function will exchange the content of these two parameters
void student_db::swap(int i,int j)
{
student temp;
temp=student_collect[i];
student_collect[i]=student_collect[j];
student_collect[j]=temp;
}
// The function will save students sorted by social security number (in ascending order).
// The parameter that is called datanumber will tell the fuction how many loop does it need.
void student_db::SortBySSN()
{
for(int i=0;i<how_many_in_database;i++)
{
for(int j=i+1;j<how_many_in_database;j++)
{
if(student_collect[i].show_ssn() > student_collect[j].show_ssn())
swap(i,j);
}
}
}
// 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.
for(int i=0;i<max_people_in_database;i++) //reading data to database
{
if(Openfile==NULL) //leaving for loop if there is nothing in the file.
break;
Openfile >> temp_use;
student_collect[i].set_lastname(temp_use);
Openfile >> temp_use;
student_collect[i].set_firstname(temp_use);
Openfile >> temp_use;
student_collect[i].set_ssn(temp_use);
Openfile >> temp_use;
student_collect[i].set_phone(temp_use);
Openfile >> temp_int_use;
student_collect[i].set_age(temp_int_use);
how_many_in_database=i; //the variable will record how many data in database.
}
SortBySSN();
return true;
}
}

// This is my new class.
//filename: studentDB.h
class student_db
{
public:
// add a student to the database – return true if successful, false otherwise
bool add_Student(std::string , std::string , std::string , int , 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 sorted by SSN or Last Name.
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 save students sorted by social security number (in ascending order).
// The parameter that is called datanumber will tell the fuction how many loop does it need.
void SortBySSN();
// The function will exchange the content of these two parameters
void swap(int,int);
//The function that use binary search algorithm will return -1 if it can not find same SSN in the array or return index if it find the SSN.
//The first parameter tell the function SSN that the function need to check.
//The second parameter tell the function how many loop it should do.
int searchSSN(std::string);
std::ifstream Openfile;
std::ofstream Savefile;
int how_many_in_database; //recording how many data in the database.
int max_people_in_database;
std::string input_filename; //input_filename record the inputfile name.
std::string output_filename; //output_filename record the outfilename;
student student_collect[50]; //You can change your max number of the people here.
};

//I define the function of the student class here.----------------------------------------------
//filename:student.cpp
#include <iostream>
#include "student.h"
//--------------------------------------------------------------------------------
// The following functions declare for the class of student.
//--------------------------------------------------------------------------------
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;
}
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;
}


// student class is my basic class------------------------------------------
//filename:student.h
class student
{
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();
private:
std::string lastname;
std::string firstname;
std::string ssn;
int age;
std::string phone;
};

AnswerRe: How to define an array of the object in class ( with code ) Pin
Joaquín M López Muñoz2-Nov-04 6:50
Joaquín M López Muñoz2-Nov-04 6:50 
GeneralRe: How to define an array of the object in class ( with code ) Pin
Cramp2-Nov-04 7:57
Cramp2-Nov-04 7:57 
GeneralBinary Image formation Pin
Jaso_O2-Nov-04 5:52
Jaso_O2-Nov-04 5:52 
GeneralRe: Not easy Pin
Budric B.2-Nov-04 6:50
Budric B.2-Nov-04 6:50 
GeneralRe: Not easy Pin
Jaso_O2-Nov-04 8:13
Jaso_O2-Nov-04 8:13 
GeneralSDI Project - Tool Bar Question. Pin
jerry1211a2-Nov-04 4:36
jerry1211a2-Nov-04 4:36 
GeneralRe: SDI Project - Tool Bar Question. Pin
Jack Puppy2-Nov-04 14:45
Jack Puppy2-Nov-04 14:45 
Generaldatas between dialogs Pin
toxcct2-Nov-04 4:21
toxcct2-Nov-04 4:21 
GeneralRe: datas between dialogs Pin
valikac2-Nov-04 5:25
valikac2-Nov-04 5:25 
GeneralRe: datas between dialogs Pin
David Crow2-Nov-04 7:05
David Crow2-Nov-04 7:05 
GeneralRe: datas between dialogs Pin
toxcct18-Nov-04 23:30
toxcct18-Nov-04 23:30 
GeneralRe: datas between dialogs Pin
David Crow19-Nov-04 2:44
David Crow19-Nov-04 2:44 
GeneralShow methods at programming Pin
dancindoc2-Nov-04 3:13
dancindoc2-Nov-04 3:13 
GeneralRe: Show methods at programming Pin
David Crow2-Nov-04 3:32
David Crow2-Nov-04 3:32 
GeneralCBUTTON in MFC Pin
madretierra2-Nov-04 3:10
madretierra2-Nov-04 3:10 
GeneralRe: CBUTTON in MFC Pin
Cedric Moonen2-Nov-04 3:20
Cedric Moonen2-Nov-04 3:20 
GeneralRe: CBUTTON in MFC Pin
madretierra2-Nov-04 3:26
madretierra2-Nov-04 3:26 

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.