Weird text at the end for Bleep project even successful execution

Hi,

I am referring to the Bleep project. The code below compiled and executed perfectly as intended. However, there are weird text at the end of the execution. You can try to run the codebyte below. Does anyone know why?

Thanks

#include<iostream> #include<string> #include<vector> void bleep(std::string &a, std::string &b); int main(){ std::string word = "brocoli"; std::string text = "let's eat brocoli if you like brocoli \n"; bleep(word, text); return 0; } void bleep(std::string &a, std::string &b){ /* iterate through text. Print through characters. If found space, convert character into string, then i++ and do the same for the next word. */ std::vector<std::string> text_vect; //std::cout << "Let's Bleep The Word\n\n"; for (int i = 0; i < b.length();i++){ bool test = true; std::string split_str = ""; //create new empty string to store the converted char while (b[i] != ' ' && test){ split_str = split_str + b[i];//character at position i is pushed to new string i++; } text_vect.push_back(split_str); //else if (b[i] == ' '){ //test = false; //} } for (int i = 0;i < text_vect.size();i++){ std::string empty_str; //create empty string to receive string for new vector element std::string conv_str = text_vect[i]; if (conv_str == a){ for (int j = 0;j < conv_str.length(); j++){ empty_str += '*'; } text_vect[i] = empty_str; } } std::string final_str; for (int i = 0; i < text_vect.size();i++){ final_str += (text_vect[i] + ' '); } std::cout << final_str << "\n"; }

Thorough testing should be done before reaching conclusions.

I am not subscribed to Codecademy and as such can’t view the project specifications, so I can’t comment on how well your program meets the specs.
But in the code posted by you, your program runs into problems when the last character of the text string is something other than space " " i.e. your program works fine for a string like "abc def " (last character is space) but not for a string like "abc def g" (last character is something other than space)

Your text string is:
std::string text = "let's eat brocoli if you like brocoli \n";

The newline character "\n" is also a character e.g. The length of "Hello\n" is 6.
So, the last character of your text string is something other than space.
You do have the condition i < b.length() in your first for loop to avoid going out of bounds, but within the for loop you have a while loop as well which advances the loop counter i as you read in a word.
The length of your text string is 39 with the last character being at index 38 i.e. b[38] is "\n"
So, when i is 38 and you reach the while loop with the condition while (b[i] != ' ' && test), this evaluates as true because '\n' is not equal to ' '. So, you read in this character to split_str and then advance the counter i++ within your while loop and go to the next iteration of your while loop. Now index 39 should be out of bounds, but C/C++ won’t throw an error or alert you that you are going out of bounds. In many other languages e.g. Python, an error will be thrown and program will crash if your index is out of range. In C/C++, the onus is on the programmer to respect the range/bounds. If you go out of bounds, you will see undefined behavior (your output could be gibberish or some other unpredictable behavior).
b[39] is beyond the bound of your text string. What will happen? Strange things. There may be something in the memory that b[39] is trying to access. If that thing is something other than ' ', the while condition will evaluate as true and you will add whatever is in that memory location to your split_str. You will keep running the while loop until either you luckily encounter a memory location which has ' ' stored in it OR something other happens such as a crash.
That is why you are seeing the weird text. Try looking at the length of your final_str. You will see that the weird text is part of your final string.


If you edit your while condition to:
while (b[i] != ' ' && i < test && i < b.length())
I think your code will work.


P.S. Your final_str will have an additional space at the end because of your statement:
final_str += (text_vect[i] + ' ')
which adds a blank space after the last character of the final string.