Tuesday, April 30, 2019

c++ - use of: while (cin >> x) and eof



I am not so sure how the works I suppose is my root problem here. I've read a few previous posts on while(cin>>x) but nothing seems to answer my question really.
I am using this loop to read in some text data:



while (cin >> x){
searchText.push_back(x);
}



but then later in the code I am trying to read in a single word using:



cout << "Please enter your target word: ";
string targetWord;
cin >> targetWord;


but the above while loop/ eof seems to scupper the 2nd code snippet (if I move the 2nd code snippet up above it all works fine, but obviously that is not what im trying to do)




EDIT
Here is the full code for clarity:



int main()
{
// ask for the target word
// cout << "Please enter your target word: ";
// string targetWord;
// cin >> targetWord;


// ask for and read the search text
cout << "Enter the complete search text, "
"followed by end-of-file: ";
vector searchText;
string x;
while (cin >> x){
searchText.push_back(x);
}

// check that some text was entered

typedef vector::size_type vec_sz;
vec_sz size = searchText.size();
if (size == 0){
cout << endl << "You must enter some text. "
"Please try again." << endl;
return 1;
}

// ask for the target word
cin.clear();

cout << "";
cout << "Please enter your target word: ";
string targetWord;
cin >> targetWord;


int count = 0;
for (int i=0; i if (targetWord == searchText[i]){
count++;

}
}

cout << "The target word [" << targetWord << "] was "
"in the search text " << count << " times." << endl;

return 0;
}



I am just trying to take in some text from the user... then a search word and see how many times the word appears in the entered text (pretty simple!)



I know I could do it differently but the question here is more about how can I use the cout/ cin stream again after it has had an EOF in it previously


Answer



When cin (or any other std::stream) hits an end of file, it sets a status to indicate that this has happened.



To reset this status, you need to call cin.clear();, which will clear any "bad" state, and make sure the stream is "ok" to use again. This also applies if you are reading from a file, and want to restart from the beginning.



Edit: I just took the code posted above, and ran it on my machine, adding at the top




#include 
#include

using namespace std;


This following is the compile and run:



$ g++ -Wall words.cpp 
words.cpp: In function ‘int main()’:

words.cpp:40:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
$ ./a.out
Enter the complete search text, followed by end-of-file: aa bb cc [CTRL-D]
Please enter your target word: aa
The target word [aa] was in the search text 1 times.


which is what I expected to see...



Edit2: For completeness: The "success rate" of using cin.clear() will depend on the implementation. A more reliable solution is to use a different way to mark the end of the stream of words in the first phase of the program. One could use a single "." or "!" or some other thing that isn't supposed to be in a "word" - or something longer, such as "&&@&&", but that makes it hard to type and remember when one is 15 pages into the input.



No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...