0

There is an exercise which dynamically asks for user input and stores in a vector, but I don't know how to end a string input. The book says it is Ctrl+Z but it doesn't work. I am using visual studio 2019 and I know it should work because when I change the variables for integers it does.

int main(void) {
    std::vector<std::string> words;

    for (std::string palabras; std::cin >> palabras;)
        words.push_back(palabras);

    std::string ban = "broccoli";

    for (std::string x : words)
        if (x == ban) std::cout << "Bleep!" << '\n';
        else std::cout << x << '\n';
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • `for` is not proper for these situations. Use `while` instead of `for`. A `for` loop is generally used when you know how many iterations of the loop you want to execute - or it is calculable. A `while` loop, or `do...while` loop is more appropriate here than a `for` loop because you do not know how many times the user wants to execute the loop. – Andy Aug 01 '20 at 00:32
  • 1
    Try Hitting `` to flush the buffer then hit `-z` to send the end of stream signal. – Martin York Aug 01 '20 at 00:36
  • I did that, but had to hit after the CTRL+Z. Thanks for the info – Andy Aug 01 '20 at 00:38
  • Another way to do this is to pipe data to the stdin externally `echo "one two three four" > app.exe` – Martin York Aug 01 '20 at 00:50
  • @Andy I disagree. I think a for loop is very appropriate here since it allows us to conveniently limit the scope of the string variable, which is only used within the loop. A for loop is often used for a known number of iterations, but there's no reason not to use it for other stuff. – eesiraed Aug 01 '20 at 04:03
  • related: https://stackoverflow.com/questions/6791520/if-cin-x-why-can-you-use-that-condition – OrenIshShalom Aug 01 '20 at 04:12

2 Answers2

0

Keep things simple: don't use the return value of std::cin as the for loop condition, unless you're sure what to expect. Here is a simple program that does what you want without using a loop. It would be a good exercise to make that work inside a loop.

#include <iostream>
#include <string>
int main(int argc, char **argv)
{
    std::string lovely_str;
    std::cout << "Enter a string: ";
    std::cin >> lovely_str;
    std::cout << "got: " << lovely_str << "\n";
    return 0;
}

If you insist on using your original program you can use ctrl+d to signal the end of read strings

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • If the OP intends to read strings until an error or EOF using `std::cin >> palabras` is perfectly fine. – eesiraed Aug 01 '20 at 04:06
  • @BessieTheCow the author has troubles reading from stdin, because he is using a cast of `std::istream` to `bool` which he seeks to make false. I merely suggested to write loops with straightforward conditions – OrenIshShalom Aug 01 '20 at 04:11
  • Using the read-from stream as the loop condition is a perfectly fine if not recommended way of reading to EOF. The OP knows what the code is doing and simply couldn't figure out how to send EOF on his system, which you did not address that at all. – eesiraed Aug 01 '20 at 04:13
  • @BessieTheCow OK, addressed the EOF issue too – OrenIshShalom Aug 01 '20 at 04:33
0

Take some help of std::istringstream & make life easier (notice comments):

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main(void) {
    // To store the entire line of input
    std::string input;
    // To store the split words
    std::vector<std::string> words;
    // Temporary variable to iterate through
    std::string temp;
    // Constant string to be used
    const std::string ban = "broccoli";

    std::cout << "Enter some words: ";
    std::getline(std::cin, input);

    // Here we go
    std::istringstream iss(input);

    // Separating each words space, e.g. apple <sp> banana => vector apple|banana
    while (iss >> temp)
        words.push_back(temp);

    // Using reference as for-each
    for (auto& i : words)
        if (i == ban) i = "bleep!";

    // Printing the modified vector
    for (auto& i : words) std::cout << i << ' ';

    std::cout << std::endl;

    return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34