Hello frog1990,
A little rework of your code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <string>
int main()
{
std::string str; // <--- Not used at this point.
char symbol{}; // <--- Initialize variable. A good name makes the code more understandable.
std::string code;
std::cout << "Enter text here: ";
cin >> code;
std::cout << "You entered: ";
std::cout << code << '\n';
std::cout << "Choose symbol to replace with *: ";
cin >> symbol;
std::cout << Text: << '\n';
return 0; // <--- Not required, but makes a good break point.
}
|
I have not tested this yet and line 20 will be an error.
You do not need all the blank lines that you have. It makes it look like there is something missing.
It is also best to include the header files with your code, so that it can be compiled and tested. Also others can check to see if you have included something that you do not need or is you have missed something.
Try and prefer using the new line (\n) over the "endl" whenever possible. The "endl" is a function that takes time to call and complete. The more you have you may notice the program running slower.
A prompt tends to work better as:
std::cout << "Enter text here: ";
. Without the "\n" or "endl" the "cin" is on the same line as the prompt. The "cin" following the "cout" will flush the output buffer before the "cin" will take any input.
After you fix line 20 you could go through "code" 1 character at a time and if it matches "symbol" replace it with the "*". Or use the strings find function to find every occurrence. As
Furry Guy mentioned.
Andy