int main() { vector<Country> readCountryInfo(const string& filename); // Creating empty vector vector<Country> myVector; // Opening file ifstream in; in.open("worldpop.txt"); if (in.fail()) { throw invalid_argument("invalid file name"); } while (in) { char buffer; // Character buffer int num; // Integer to hold population string countryName; // Add character buffer to create name while (in.get(buffer)) { // Check if buffer is a digit if (isdigit(buffer)) { in.unget(); in >> num; } // Check if buffer is an alphabetical character else if (isalpha(buffer) || (buffer == ' ' && isalpha(in.peek()))) { countryName += buffer; } // Checking for punctuation to print else if (ispunct(buffer)) { countryName += buffer; } // Check for new line or end of file else if (buffer == '\n' || in.eof()) { // Break so it doesn't grab next char from inFile when running loop break; } } Country newCountry = {countryName, num}; myVector.push_back(newCountry); } return myVector; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)