Hey guy i am pretty much a novice in C++ and i have been using Bjarne Stroustrup's Book, Programing Principles in C++ 2nd edition. There was one task where i was asked to create a vector of strings and input the strings using the push_back member function and then define a string called disliked and if that word is typed in it should output bleep alongside the other inputed words but i keep getting nonsensical output as i believe my approach did not address the problem fully.
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
inlinevoid keep_window_open() {char ch; cin >> ch;}
int main()
{
vector<string> Words;
const string disliked = "Brocolli"; // ideally, const
// end input with eof: ctrl+D (unix/unix-clone) / ctrl+Z (windows)
for( string text; cin >> text; ) Words.push_back(text);
cout << "Number of Words: " << Words.size() << '\n';
for( std::size_t i = 0; i < Words.size(); ++i ) // for each word in the vector
{
// if there is no earlier word (i==0)
// or if this word is not identical to the previous word
if( i == 0 || Words[i-1] != Words[i] )
{
// if this word is the disliked word, print "Bleep"
if( Words[i] == disliked ) cout << "Bleep!!!" << '\n';
// otherwise print the word
else cout << Words[i] << '\n' ;
}
}
}
JLBorges Thank you so much, i guess the part i miss was to nest the if statement which carries the if-else, i ran the code and it did exactly what i wanted it to do, thanks so much for your help, i greatly appreciate and look forward to improving my overall C++ experience.