/* File: input9.cpp Testing file I/O using >> with strings. */ #include #include #include #include #include #include // so you can use vector using namespace std ; int main() { string oneLine ; string str1, str2, str3, str4, str5, str6, str7, str8, str9 ; string str10, str11 ; long int population ; float litRate ; vector names ; ifstream ifile("2013WorldBankEducationCensusData.txt") ; getline(ifile, oneLine) ; while(true) { getline(ifile, oneLine) ; // cout << oneLine << endl ; // if ( ifile.eof() ) break ; if ( ifile.fail() ) break ; istringstream istrm(oneLine) ; // new one each loop! istrm >> str1 ; istrm >> str2 ; istrm >> str3 ; istrm >> str4 ; istrm >> str5 ; istrm >> str6 ; istrm >> str7 ; istrm >> str8 ; istrm >> str9 ; istrm >> str10 ; istrm >> str11 ; cout << "country = '" << str1 << "' " ; population = atol(str2.c_str()) ; cout << "population = " << population << " " ; if ( str3 == "N/A" ) { litRate = -1.0 ; } else { litRate = atof(str3.c_str()) ; } cout << "literacy rate = " << litRate << endl ; names.push_back(str1) ; // add country name to vector /* cout << "str2 = '" << str2 << "'\n" ; cout << "str3 = '" << str3 << "'\n" ; cout << "str4 = '" << str4 << "'\n" ; cout << "str5 = '" << str5 << "'\n" ; cout << "str6 = '" << str6 << "'\n" ; cout << "str7 = '" << str7 << "'\n" ; cout << "str8 = '" << str8 << "'\n" ; cout << "str9 = '" << str9 << "'\n" ; */ } ifile.close() ; // print out country names from vector // cout << "\n\n\n**** Vector test *****\n" ; int n = names.size() ; for (int i = 0 ; i < n ; i++) { cout << names[i] << endl ; } }