// Lab7-Actor-Army-File.cpp #include #include #include using namespace std; #include "ColourConsole.h" using namespace ColourConsole; #include // FUNCTION PROTOTYPES (function declarations; function definitions are at the bottom of the source code) // Helper functions to handle safe keyboard input void Get(int& rnNumToInput, char* pszPrompt, int nLow, int nHigh); void Get(char* pszInput, char* pszPrompt, int nMaxNumCharacters); int GetMenuSelection(); void Wait(); //********************************************************************** // class Actor { //********************************************************************** class Actor { private: static const int nMAX_STR_LEN = 31; char szName[nMAX_STR_LEN+1]; int nStrength; int nSpeed; int nHealthPoints; public: Actor() : nStrength(0), nSpeed(0), nHealthPoints(0) { szName[0] = '\0'; } bool Read(ifstream& rifsInput) { int nStrLen; rifsInput>>nStrLen; if (nStrLen > nMAX_STR_LEN) // ensure text being entered will fit within the allocated array of char return false; if (nStrLen > 0) // if the Actor on disk has no name suppress input of rifsInput.get(szName, nStrLen+1); rifsInput>>nStrength>>nSpeed>>nHealthPoints; return true; } void Write(ofstream& rofsOutput) { rofsOutput<> szFileName; ifstream ifsIn(szFileName); if (ifsIn.fail()) { cout << "Error: File Read: Unable to open " << szFileName << endl; return 0; } while ( ! ifsIn.eof()) { if ( ! aaActors[nNumActors++].Read(ifsIn)) // terminate reading if any failure in file input break; } ifsIn.close(); return nNumActors - nOriginalNumActors; // returns the number of Actor objects added. } //********************************************************************** // int Army::Write() //********************************************************************** int Army::Write() { if (nNumActors == 0) return 0; // nothing to do since there are no Actor objects ofstream ofsOut(szFileName); // file opened in an overwrite mode if (ofsOut.fail()) { cout << "Error: File Write: Unable to open " << szFileName << endl; return 0; } for (int i=0; i> rnNumToInput; while ( ! cin.good() || rnNumToInputnHigh) { cin.clear(); cin.ignore(128,'\n'); cout << "Invalid entry.\nEnter number between " << nLow << " and " << nHigh << endl; cout << pszPrompt; cin >> rnNumToInput; } } //********************************************************************** // void Get(char* pszInput, int nMaxNumCharacters) //********************************************************************** void Get(char* pszInput, char* pszPrompt, int nMaxNumCharacters) { cout << pszPrompt; int i = 0; char c; while ((c = _getch()) != '\r') { if (c == '\b' && i > 0) { // backspace --i; // decrement index into char array, next input overwrites old putchar('\b'); // output backspace -- moves cursor backwards putchar(' '); // overwrite old screen character to erase visual putchar('\b'); // output backspace -- moves cursor backwards } else if (isprint(c) && i < nMaxNumCharacters) { putchar(c); // echo character to screen (not done by getch()) pszInput[i] = c; // capture keyboard character to char array ++i; // next index position -- get ready for next char } } pszInput[i] = '\0'; // place "end-of-string" flag after last input character cout << endl; } //********************************************************************** // void Wait() //********************************************************************** void Wait() { cout << "\nPress a key to continue . . ."; _getch(); }