C ++ typing censored for 'darn'

  • MHB
  • Thread starter adrumheiser
  • Start date
In summary, the conversation discusses an issue with a code that should output "Censored" if userInput contains the word "darn" and the code is not working as expected. The expert explains that the problem lies in the if statement, where the expression is always true unless the word "darn" is the first few characters of userInput. The expert suggests modifying the if statement to check if the return value of the string's find method is not equal to -1, which will fix the issue.
  • #1
adrumheiser
2
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
  • #2
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;
}
 
  • #3
It worked, thank you!
 

Related to C ++ typing censored for 'darn'

1. What is "darn" in C++ typing?

"Darn" is a censored term used in place of a more commonly used term in C++ typing, which is considered to be inappropriate or offensive. It is often used to refer to a data type or variable name that is considered to be offensive or vulgar.

2. How do I avoid using "darn" in my C++ code?

To avoid using "darn" in your code, it is important to be mindful of the words and terms you use when naming your variables and data types. If you are unsure of whether a term may be considered offensive, it is best to choose a different word or phrase to avoid any potential issues.

3. Can using "darn" in my code affect its functionality?

No, using "darn" in your code will not affect its functionality. The use of this term is purely for personal or professional preference and does not have any impact on the actual code itself.

4. Are there any alternative terms I can use instead of "darn" in my code?

Yes, there are many alternative terms you can use instead of "darn" in your code. It is important to choose a term that accurately describes your variable or data type while also being appropriate and respectful.

5. Is it considered bad practice to use "darn" in my C++ code?

While there is no specific rule against using "darn" in C++ code, it is generally considered to be unprofessional and disrespectful. It is best to avoid using this term and instead opt for more appropriate and respectful language in your code.

Similar threads

  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
3
Views
850
Back
Top