/* File: input6.cpp Testing file I/O using >> with strings. */ #include #include #include #include using namespace std ; int main() { string firstLine ; string str1, str2, str3, str4, str5, str6, str7, str8, str9 ; long int population ; ifstream ifile("2013WorldBankEducationCensusData.txt") ; getline(ifile, firstLine) ; while(true) { ifile >> str1 ; ifile >> str2 ; ifile >> str3 ; ifile >> str4 ; ifile >> str5 ; ifile >> str6 ; ifile >> str7 ; ifile >> str8 ; ifile >> str9 ; if ( ifile.eof() ) break ; cout << "country = '" << str1 << "' " ; // use atol() to convert to long population = atol(str2.c_str()) ; cout << "population = " << population << endl ; // use == for string comparison if ( str3 == "N/A" ) cout << " str3 has no value\n" ; /* 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() ; }