Click here to Skip to main content
15,886,643 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
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;
    
    }
Posted
Comments
[no name] 28-Sep-14 21:17pm    
Because int Main returns an integer, a vector is not an integer, it's a vector.
Sergey Alexandrovich Kryukov 29-Sep-14 1:46am    
This is the answer. Will you post it, to close the question?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900