But Result is the most important part in Datastructure2.h as the information storage and I am going to use it in main.cpp.
If I use this method to make Result inaccesible, then error cause in main.cpp because main program need to deal with it.
Sorry for the code above. I have made it better here
Code:
int main( ) {
Datastructure2 tr2;
char command = ' ';
while( true ) {
cin >> command;
// addition
if( command == 'L' ) {
string file_name = "";
cin >> file_name;
// read file
if( !addFile( tr2, file_name ) ) {
cout << "Error in reading file: " << file_name
<< endl;
}
}
// removal
else if( command == 'P' ) {
string file_name = "";
cin >> file_name;
// read the file
if( !removeFile( tr2, file_name ) ) {
cout << "Error in opening file: " << file_name
<< endl;
}
}
// print
else if( command == 'K' || command == 'T') {
int amount = 0;
if( !readNumber( amount ) || amount <= 0 || amount > tr2.size( ) ) {
cout << "Incorrect amount!" << endl;
}
else if( command == 'K' ) {
print(tr2, amount);
}
else {
printListing(tr2, amount);
}
}
// quit
else if( command == 'Q' ) {
tr2.removeAll();
return EXIT_SUCCESS;
}
else {
cout << "Unknown command: " << command << endl;
}
}
}
Code:
bool addFile( Datastructure2& tr2, string& file_name ) {
// open the file
ifstream file( file_name.c_str( ) );
// file not found
if( !file ) {
return false;
}
char separator =' ';
int index = 1;
string smudge;
// Create Result structs out of the lines in the file
while( !file.eof() ) {
Result* new_result = new Result;
getline(file, new_result->name, ':');
file >> new_result->totalresult;
int tmp =0;
for( int idx = 0; idx < THROWS; idx++ ) {
file >> separator >> tmp;
new_result->results.add( tmp );
}
while(isspace(file.peek())) {
file.get(separator);
}
while(index <= tr2.size() &&
tr2[index].totalresult > new_result->totalresult) {
index++;
}
// add new result to database
tr2.add( new_result, index );
}
return true;
}
Code:
bool removeFile( Datastructure2& tr2, string& file_name ) {
// open file
ifstream file( file_name.c_str( ) );
// file not found
if( !file ) {
return false;
}
char separator = ' ';
int index = 1;
string name = "";
int totalresult = 0;
// Go through the file
while( !file.eof() ) {
getline(file, name, ':');
file >> totalresult;
int tmp = 0;
for( int idx = 0; idx < THROWS; idx++ ) {
file >> separator >> tmp;
}
while(isspace(file.peek())) {
file.get(separator);
}
while(index < tr2.size() &&
tr2[index].totalresult > totalresult) {
index++;
}
// remove the resultfrom the data
if( tr2[index].name == name) {
tr2.remove( index );
}
}
return true;
}
Code:
void print( Datastructure2& tr2, int amount ) {
for( int index = 1; index <= amount; index++ ) {
cout << tr2[index].name << " " << tr2[index].totalresult;
for( int jndex = 1; jndex <= THROWS; jndex++ ) {
cout << " " << tr2[index].results[jndex];
}
cout << endl;
}
}
Code:
void printListing( Datastructure2& tr2, int amount ) {
for( int index = 1; index <= amount; index++ ) {
cout << tr2[index].name << " " << tr2[index].totalresult << endl;
}
}
Code:
bool readNumber( int& number) {
string tmp = "";
cin >> tmp;
istringstream ss(tmp);
if(!(ss >> number)) {
return false;
}
return true;
}