c++ - How to read words instead of characters? -


i trying read bunch of words .txt document , can manage read characters , display them yet. i'd same whole words.

my code:

#include <iostream> #include <fstream> #include <string> using namespace std;  int main() {  ifstream infile("banned.txt"); if (!infile)  {      cout << "error: ";     cout << "can't open input file\n";   }   infile >> noskipws;  while (!infile.eof())  {     char ch;     infile >> ch;      // useful check read isn't end of file     // - stops character being output @ end of loop     if (!infile.eof())     {         cout << ch << endl;     }  } system("pause"); } 

change char ch; std::string word; , infile >> ch; infile >> word; , you're done. or better loop this:

std::string word; while (infile >> word) {     cout << word << endl; } 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -