I'm going through B. Stroustrup's Programming Principles and Practice Using C++ and I encountered a problem doing one of the exercises. I'm using Windows 10 (I believe, that's relevant here). The code below is supposed to ask the user for words, which are to be printed back. However, if a word is "broccoli", then instead of "broccoli", "BLEEP" will be printed.
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
inline void keep_window_open() { char ch; cin >> ch; }
int main()
{
string bad_word = "broccoli";
cout << "type in some words\n";
vector <string> words;
for (string temp; cin >> temp; )
words.push_back(temp);
cout << "There are " << words.size() << " words in vector 'words'.\n";
for (string word : words) {
if (word != bad_word)
cout << word;
else
cout << "BLEEP";
}
keep_window_open();
return 0;
}
So, I have my program ask me for words, I print some, then hit Ctrl+Z + Enter, and nothing happens. If I do that again, the console closes. Why doesn't it print the number of words as specified by this line?
cout << "There are " << words.size() << " words in vector 'words'.\n";
Why does the program finish execution after I hit Ctrl+Z+Enter the second time? I want to understand what the problem really is here. Thank you.