0

I have a text file that has two words next to one another separated by a space. I am trying to read in the words using >> but whenever I attempt to do this, it reads in the same word.

string word1;
string word2;

ifstream fin;
fin.open("text.txt");

while (!fout.eof())
{
    fin >> word1;
    cout << word1 << endl;
    fin >> word2;
    cout << word2 << endl;
}

The first word in the file is the. The second is happy. When I try to output the words, both are the. How should I go about reading in both words as separate variables?

Raindrop7
  • 3,889
  • 3
  • 16
  • 27
TEEBQNE
  • 6,104
  • 3
  • 20
  • 37

1 Answers1

2

you can read the word like this:

while (fin >> word1 >> word2)
    cout << word1 << endl << word2 << endl;
Raindrop7
  • 3,889
  • 3
  • 16
  • 27