Cannot get this for-loop to output to console.

Hello guys. I'm working my way through C++, and I'm trying out a very basic program where the console asks you to input a word (string), and then captures that input into a 3 element vector. After that, it is meant to output all three of those elements to the user. If any of the words that was captured are the designated "disliked word", then what should be output in that index's place is the word "BLEEP!".

However, the problem I'm running into is that the second major part of the below function called "createWordList", that is, the for-loop that is meant to output a list of the items from the vector, doesn't seem to call. I'm really not sure what I'm doing wrong here. Can someone please help me see where I messed up? Thanks for your help! :)

edit: Regarding my use of the algorithm header, I know that I'm not using anything from there at the moment, but I intend to use it for something else in this code once I get what I currently have working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>     //std::cout
#include <vector>       //std::vector
#include <algorithm>    //std::find
using namespace std;


void createWordList ()
{
    string enteredWord;
    vector <string> words;
    string dislikedWord = "badWord";
    
    while (words.size() != 3)
    {
        cout << "Enter your word\n";
        cin >> enteredWord;
        words.push_back(enteredWord);
    }
    
    cout << "\n" << words.size() << "\n";
    
    for (int i = 0; i == words.size()-1; i++)
    {
        if (words[i] == dislikedWord)
        {
            words[i] = "BLEEP!";
        }
        
        cout << "\n" << words[i];
    }
}

int main()
{
    createWordList();
    
    
    return 0;
}
Last edited on
Hi,

A for loop ends when the end condition becomes false, so try this:

22
23
24
25
26
27
28
29
30
    for (int i = 0; i < words.size(); i++)
    {
        if (words[i] == dislikedWord)
        {
            words[i] = "BLEEP!";
        }
        
        cout << "\n" << words[i];
    }


Hope this works for you :+)
Oh wow. Thank you. I actually already knew that about For Loops. (I code in Javascript a lot) Not sure how I made that mistake. Must be some brain malfunction, indicating that I should take a break, lol.

Thanks again!
Topic archived. No new replies allowed.