C ++ typing censored for 'darn'

  • Context: MHB 
  • Thread starter Thread starter adrumheiser
  • Start date Start date
Click For Summary
SUMMARY

The discussion addresses a common issue in C++ programming related to string manipulation and conditional statements. The user encountered a problem where the code incorrectly printed "Censored" even when the input did not contain the word "darn." The solution involves modifying the if statement to check if the return value of the string's find method is not equal to -1, ensuring accurate detection of the substring. The corrected code snippet uses if (userInput.find("darn") != -1) to achieve the desired functionality.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Familiarity with the C++ Standard Library, specifically the string class
  • Knowledge of conditional statements in C++
  • Basic debugging skills in C++ programming
NEXT STEPS
  • Explore C++ string manipulation techniques and methods
  • Learn about error handling and debugging in C++
  • Study the differences between string::find and other string searching methods
  • Investigate best practices for writing conditional statements in C++
USEFUL FOR

Beginner C++ programmers, students working on string manipulation assignments, and anyone looking to improve their understanding of conditional logic in C++.

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")) {
count << "Censored" << endl;
}
else {
count << 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!
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 14 ·
Replies
14
Views
34K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
1K
  • · Replies 5 ·
Replies
5
Views
4K