Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

C++ code

jj10

Recruit
Basic Member
Messages
11
Reaction score
0
Points
16
sino online na mgaling sa program. c++ ang inaaral ko ngayon. oa cgeck naman kung ano mali ko sa code kc ayaw mag run.

// A simple example of Student records ...
// using a C++ 'list' container to hold each 'Student' element
// 1. adds info from keyboard into a list of 'Student' records (using push_back)

// 2. shows the 'records' in memory on the screen
// 3. writes to file ... all the records in memory
// 4. reads from file and stores all the records in memory (in a C++ list)
// 5. allows edit/erase ... to a Student record
// 6. sorts Student records by student name ...
// 7. sorts Student records by student id number ...
// Note:
// * this program allows only unique student id's for each record
// * this program is set to update its file, (if there were any changes),
// before quitting

#include <fstream>
#include <iostream>
#include <string>
#include <list>
#include <cctype>

// Globals ...
using namespace std;
const char THIS_TERM[] = "fall.txt"; // file name for this term's records
const string HEADER = "\nWhat do you want to do ?\n\n"
"\t1. A dd a new student name and id ? \n"
"\t2. E dit/E rase a student record ? \n"
"\t3. S ort a student record ? \n"
"\t4. V iew all in memory at present ? \n"
"\t5. U pdate file with data currently in memory ? \n"
"\t6. L oad in data from file ? \n"
"\t7. e X it program ? ";

class Student
{
public:
// constructors ...
Student(){}
Student(string nam, string num ) { name=nam; id=num; }

// setters ...
void setName(string nam){ name = nam; }
void setID(string num) { id = num; }

// getters ...
string getName() { return name; }
string getID() { return id; }
private:
string name, // add any needed info here, just like in a C/C++ 'struct'
id;
};


// functions used by main to process a list of Student records

// returns a valid iterator if ID is used already ... otherwise returns NULL
list< Student >::iterator existID( list< Student >& term, string& ID )
{
list< Student >::iterator it;
for( it = term.begin(); it != term.end(); ++it )
{
if( it->getID() == ID )
return it;
}

return NULL;
}

// adds Student records to end of end of list of Student records ... 'term'
// gets input from keyboard ...
int newStud( list< Student >& term )
{
cout << "\nEnter an empty record to exit this 'Input Loop' ..." << endl;
int count = 0, reply;
string nam, num;
for( ;; ) // loop forever until break ...
{
cout << "\nID : ";
getline(cin, num);
if( existID( term, num ) != NULL )
{
cout << "\nThat 'id' " << num << " already exits ... " << endl;
continue; // from the top of the forever loop right now
}
cout << "Name : ";
getline(cin, nam);
if( nam=="" || num=="")
break;

cout << "Add or Redo (a/r) ? ";
reply=cin.get();
cin.sync();
if ( toupper(reply)!='A' )
{
cout << "Aborted ..." << endl;
continue;
}

// ok ... create and add this record to the end of the list ...
term.push_back( Student(nam, num) );
++count;
cout << "Added ..." << endl;
}
return count;
}

// shows (to console screen) all student records in list container ... 'term'
void viewStuds( list< Student >& term )
{
list< Student >::iterator it;
int i = 0;
for( it = term.begin(); it != term.end(); ++it )
{
cout << ++i << ".\n"
<< "Name : " << it->getName() << endl
<< "Number : " << it->getID() << endl;
}
}

// file all records in memory ... create/overwrite file with name 'THIS_TERM'
int fileStuds( list< Student >& term )
{
ofstream fout( THIS_TERM ); // recall 'THIS_TERM' is a Global variable
if ( !fout )
{
cout << "\nError attempting to open file ..." << endl;
return -1; // report error condition ...
}

// else ...
list< Student >::iterator it;
int i = 0;
for( it = term.begin(); it != term.end(); ++it )
{
fout << it->getName() << "," << it->getID() << endl;
++i;
}

fout.close();

if( i == (int)term.size() )
cout << "\nAll " << i << " records filed ok." << endl;
else
cout << "\nOnly " << i << " of " << term.size()
<< " records were written ..." << endl;

return i; // report success ... i.e. report count of records filed
}

// reads in all Student records from file 'THIS_TERM' ... if it exists
// returns -1 if no file exists; else returns the number of records read
int readStuds( list< Student >& term )
{
ifstream fin( THIS_TERM ); // recall THIS_TERM is a Global variable
if ( !fin )
{
cout << "Error attempting to open file ... "
<< "(Perhaps it dosen't exist yet?)" << endl;
return -1; // report error condition ...
}

// else ... check existing term.size() first before re-setting?
if( term.size() ) // i.e. if not == 0 ...
{
cout << "\nDo you want over-write the " << term.size()
<< " records in memory (y/n) ? " << flush;
int reply = toupper( cin.get() );
cin.sync();
if( reply != 'Y' )
{
cout << "Aborted ... " << flush;
return 0;
}
}

// else ... if reach here ...
list< Student >temp;
term = temp; // set term to null ...
string nam, num;
int i;
for( i=0; getline( fin, nam, ',' ); ++i ) //first get 1st string (up to ',')
{
getline( fin, num, '\n' ); // then get rest of line (up to '\n')
term.push_back( Student(nam, num) ); // construct and add new Student
}
fin.close();
return i; // report success? ... i.e. return the record count ...
}

// returns 'true' is a record was edited or erased; otherwise returns false
bool editStud( list< Student > &term )
{
cout << "\nEnter an empty record to exit this 'Edit/Erase Function' ..."
<< "\nEnter the ID of the student record to edit ? " << flush;
string idStr, nam;
getline( cin, idStr );

list< Student >::iterator i, index;
i = existID( term, idStr );
if( i == NULL )
{
cout << "This '" << idStr << "' does not exist." << endl;
return false;
}

// else ... show ... and ask if ok to edit ...
cout << "Name : " << i->getName() << endl
<< "Number : " << i->getID() << endl

<< "Ok to edit/erase (y/n) ? " << flush;

int reply = toupper( cin.get() );
cin.sync();
if( reply != 'Y' )
{
cout << "Aborted ... " << endl;
return false;
}

cout << "\nDo you want to erase this record (y/n) ? " << flush;
reply = toupper( cin.get() );
cin.sync();
if( reply == 'Y' )
{
term.erase( i );
cout << "Erased ..." << endl;
return true;
}


// else ...

cout << "\nNew ID : ";
getline(cin, idStr);
index = existID( term, idStr );
if( index != NULL && index!= i )
{
cout << "\nThat " << idStr << " already exits ... " << endl;
return false; // exit to menu now ...
}

cout << "New Name : ";
getline(cin, nam);
if( nam=="" || idStr=="")
return false;

cout << "Ok or Redo (o/r) ? ";
reply=cin.get();
cin.sync();
if( toupper(reply)!='O' ) // cap 'O' ... as in Ok
{
cout << "Aborted ..." << endl;
return false;
}

// ok ... do edit
i->setName( nam );
i->setID( idStr );
cout << "Edited ..." << endl;
return true;
}

// name comparison here is NOT case sensitive ...
bool compare_nocaseName(Student& first, Student& second)
{
unsigned int i=0;
while( i<first.getName().length() && i<second.getName().length() )
{
if( tolower(first.getName()) < tolower(second.getName()) ) return true;
else if( tolower(first.getName()) > tolower(second.getName()) ) return false;
++i;
}
if( first.getName().length() < second.getName().length() ) return true;
else return first.getID() < second.getID();;
}

bool compare_id(Student& first, Student& second)
{
return first.getID() < second.getID();
}


void pauseForEnter()
{
cout << "\nPress 'Enter' to continue ... " << flush;
cin.sync();
cin.get();
}



int main()
{
// create a 'fall list' to hold student names and ids for the 'Fall Term'
// also holds number of records, via 'fall.size()'
list <Student > fall;

// now get all records from file ... if it exists ?
int count = readStuds( fall );
if( count >= 0 )
cout << count << " student record(s) read into memory ..." << endl;
else
cout <<"(The file will be created when some student records exist.)"
<<endl;

bool changes = false; // set 'update file flag' to initial value ...
int reply;
for( ;; ) // loop forever ... until break ...
{
cout << HEADER;
reply = toupper(cin.get());
cin.sync(); // flush cin stream ...

if( reply == '1' || reply == 'A' )
{
int numStuds = newStud( fall );
cout << endl << numStuds << " student record(s) added ..."
<< " The total number of student records now is "
<< fall.size() << endl;
if( numStuds )
changes = true; // if >0 update bool variable changes
}
else if( reply == '2' || reply == 'E' )
{
if( editStud( fall ) )
changes = true;
}
else if( reply == '3' || reply == 'S' )
{
cout << "\nSort by id or name (i/n) ? " << flush;
reply = toupper( cin.get() );
cin.sync();
cout << "Now sorted in memory by ";
if( reply == 'I' )
{
fall.sort(compare_id);
cout << "id. ";
}
else
{
fall.sort(compare_nocaseName);
cout << "name. ";
}
cout << "Update file (y/n) ? " << flush;
reply = toupper( cin.get() );
cin.sync();
if( reply == 'Y' )
{
changes = true;
cout << "File to be updated ...\n";
}
}
else if( reply == '4' || reply == 'V' )
viewStuds( fall );
else if( reply == '5' || reply == 'U' )
{
if( !changes )
cout << "\nNo changes to file ..." << endl;
else
{
cout << "Are you sure you want to update the file (y/n) ? "
<< flush;
reply = toupper( cin.get() );
cin.sync();
if( reply == 'Y' )
{
if( fileStuds( fall ) != (int)fall.size() )
cout << "\nERROR! NOT all records were filed!" << endl;
else
{
changes = false;// update file flag ...
cout << "File write operation confirmed." << endl;
}
}
}
}
else if( reply == '6' || reply == 'L' )
{
int condition = readStuds( fall );
if( condition >= 0 )
cout << "\nThere were " << condition
<< " student records read into memory from file." << endl;
}
else if( reply == '7' || reply == 'X' )
{
if( changes ) // then ...
fileStuds( fall );
break; // and exit program ...
}
else
{
cout << "\nThis choice not implemented yet ... " << endl;
} // end of if( reply == ? ) switch structure ...

} // end of forever loop ...

// if you have a Win OS ... show structure of file
string tmpSee = " ";
tmpSee = "notepad" + tmpSee + THIS_TERM;
system( tmpSee.c_str() );
}
 
Back
Top Bottom