Search code examples
c++vectorterminate

(C++) Learning vectors - problems with coding/input stream termination/comparing vectors


I'm beginner learning my first programming language - C++ - from Bjarne Stroustrup's book "Programming Principles and Practice Using C++". Chapter 4 talks about vectors. Everything explained before I would get easily and code would always work properly, until now.

The code I write doesn't function at all. The following code was made for little exercise, where the input is read in and prints the words out, bleeping out the disliked ones.

#include "std_lib_facilities.h"

int main() {
    vector<string>text;
    string disliked = "cat";

    for (string word; cin >> word;) {       
        text.push_back(word);
    }

    for (int a = 0; a < text.size(); ++a) {
            if (text[a] != disliked) {
                cout << text[a] << endl;
            }
            else {
                cout << "BLEEP\n";
            }
        }
    keep_window_open();
}

My first idea was to create another vector, vector<string>disliked ={"cat", ...} , for disliked words, but then if (text[x] != disliked) didn't seem like a way of comparing elements from each vector (at least it was warning me about operator and operands mismatch). Is there a way for that?

But back to the code: with some modifications and without any disliked word in the input, the program would run sometimes. Still I can't manage to meet the main purpose. And perhaps, the actual mistake is in the input termination. Ctrl+Z doesn't work for me but just inputs a character. And somehow Ctrl+C happened to work properly (if there were no disliked words).

So here come the actual questions:

  1. Is the code correct? (since I can't check it myself while I might have been terminating the input improperly entire time)

  2. How can I terminate input any other way, considering that Ctrl+Z only adds a character to the input?

  3. Is there a way of making this program work by comparing the "input" vector with the "disliked" vector?


Solution

  • Is the code correct? (since I can't check it myself while I might have been terminating the input improperly entire time)

    Seems to work for me.

    How can I terminate input any other way, considering that Ctrl+Z only adds a character to the input?

    I use Ctrl-D to mark end of line.

    Is there a way of making this program work by comparing the "input" vector with the "disliked" vector?

    Usually when you compare types (with == and !=) they have the same type (or the compiler can convert one type to the same type as the other type (but that's for the pedantic here; for beginners its best to think of comparison comparing objects of the same type)).

    vector<string> text;
    string         disliked = "cat";
    
    // STUFF
    if (text[x] != disliked) // disliked is a string
                             // text[x]  is a string (because we are accessing the `x` element.
    

    If we change disliked to a vector:

    vector<string> text;
    vector<string> disliked = "cat";
    
    // STUFF
    if (text[x] != disliked) // disliked is a vector<string>
                             // text[x]  is a string
    

    Since the types do not match they are hard to compare. So you need to loop over all the elements in disliked to see if you can find the word.

    bool found = false;
    for(std::size_t loop = 0; loop < disliked.size(); ++loop) {
        if (text[x] == disliked[loop) {  // Types are the same here.
            found = true;
            break;
        }
    }
    if (!found) {
    

    There are techniques to make the above compact. If you are just started this may be a bit early for this, but for completness I will add it here:

    bool found = std::find(std::begin(disliked), std::end(disliked), text[x]) != std::end(disliked);
    if (!found) {