Thread: strstr problem...

  1. #1
    Registered User
    Join Date
    Sep 2006

    strstr problem...

    Okay, I have to do some mini-review for my programming class, but I haven't touched C++ in ~6months or so, so I'm a bit rusty.

    I have to have the user input text, then reject it if it contains profanity.

    this is what I have so far, but I can't seem to get it to work. As you can see, I have the words it's checking for in a seperate file so it's easy to update.

    #include <string.h>
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    using namespace std;
    char str[100];
    string prof1, prof2, prof3, prof4, prof5;
    int main()
    	ifstream fin;
        char *pdest;
        int  result;
    	fin >> prof1 >> prof2 >> prof3 >> prof4 >> prof5;
    	string words[5] = {prof1, prof2, prof3, prof4, prof5};
    	cout << "Enter text: ";
    	cin >> str;
    	for(int a=1; a<5; a++)
        pdest = strstr( str, words[a] );
        if ( pdest != NULL )
           cout << "Profanity found!";
    	   return 0;
           cout << "No profanity found\n";
    	return 0;

    any help would be appreciated. I can't figure this out, it should be pretty easy, and it's driving me nuts!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Out of scope
    Use the std::string class not character arrays. They're better and they contain their own substr() method which is significantly easier to work with.

    Now for some fundamental problems with your code:

    You're mixing your headers up... <stdio.h> and <string.h> aren't under the standard namespace and are both deprecated (actually they aren't standard C++, at all). You should be using <cstring> for the latter and the former has no reason to be there, at all. It's not being used (it's counterpart is <cstdio> however).

    You're also declaring string objects which aren't defined in <string.h>, they're defined in <string>. You likely lucked out because one of your other libraries includes <string>.

    Your closing brace is missing on your for loop.

    You have an unnecessary return in your if statement.

    ...and this is just bickering, but I would be explicitly closing my ifstream before returning.
    Sent from my iPadŽ

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > for(int a=1; a<5; a++)
    Arrays also start at 0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2005
    >> I can't seem to get it to work.
    What is the problem? A compile error? It always outputs not found? What input are you giving it?

    >> Your closing brace is missing on your for loop.
    I don't think so, the else closing brace is actually for the for loop (the else has no opening brace). Better indentation and more consistent bracing would make it a lot easier to see.

    >> actually they aren't standard C++, at all
    Yes, they are standard. They are just deprecated. They must work on all standards-conformant compilers despite the fact that the C++ versions are preferred.

    >> I would be explicitly closing my ifstream before returning.
    I wouldn't. But I wouldn't quibble either way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strstr problem...
    By Sebastiani in forum C Programming
    Replies: 6
    Last Post: 10-09-2008, 07:11 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM