MHB C ++ typing censored for 'darn'

  • Thread starter Thread starter adrumheiser
  • Start date Start date
AI Thread Summary
The discussion revolves around a C++ coding issue where the user struggles to correctly censor the word "darn" in a string input. The original code incorrectly uses the `find` method, leading to unintended behavior where "Censored" is printed even when "darn" is not present. The key point of the solution is to check if the result of `userInput.find("darn")` is not equal to `string::npos` (which indicates that the substring was not found). The corrected condition should be `if (userInput.find("darn") != string::npos)`, ensuring that "Censored" is only printed when "darn" is actually in the input. The user confirms that this modification resolves the issue.
adrumheiser
Messages
2
Reaction score
0
Hi all, I am new to c++ and need help with my homework. I have tried many times and somehow cannot get it right. My following code works for 'darn' but when tested with "Dang, that was scary!," prints censored also. Any hints?

Print "Censored" if userInput contains the word "darn", else print userInput. End with newline.

#include <iostream>
#include <string>
using namespace std;

int main() {
string userInput;

userInput = "That darn cat.";

if (userInput.find("darn")) {
cout << "Censored" << endl;
}
else {
cout << userInput << endl;
}
return 0;
}
 
Technology news on Phys.org
Hi,
Your problem is in the statement (BTW use the code tag to make your code readable):
Code:
if (userInput.find("darn")) {
   cout << "Censored" << endl;
}
else {
   cout << userInput << endl;
}
As in all of C++, if the expression userInput.find("darn") has a non-zero value, the if statement is deemed to be true. Now the return value of string's find method is the position (index) of the first occurrence of parameter target in case target is actually a substring; this can range from 0 to the length of the string-1. If target is not a substring, the return value is string::npos. This last value is actually -1, which is non-zero. So in your construct userInput.find("darn") is always true unless "darn" is the first few characters of userInput. This is definitely not what you want. So modify your if statement with
Code:
if (userInput.find("darn") != -1) {
   cout << "Censored" << endl;
}
else {
   cout << userInput << endl;
}
 
It worked, thank you!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
7
Views
7K
Replies
4
Views
6K
Replies
14
Views
34K
Replies
1
Views
5K
Replies
5
Views
3K
Back
Top