• Lucid Dreaming - Dream Views




    Page 7 of 7 FirstFirst ... 5 6 7
    Results 151 to 165 of 165
    1. #151
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      One thing I was wondering. How would you get it to detect a word, or character within a user input?

      Like, I know there is this:

      Code:
      if (userinput == "fuck");
      cout << "omg you swore, you basterd" << endl;
      But that means the user has to input "fuck" only, and nothing else.

      The way I want it would be more like if the person wrote a whole sentence that included the word "fuck", then it would say "omg you swore..."

      Just as an example, of course.

    2. #152
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Code:
      std::string user_input;
      std::cin >> user_input;
      
      found_fuck = user_input.find("fuck");
      if (found_fuck != std::string::npos)
      {
          std::cout << "omg you swore, you basterd" << std::endl;
      }
      (\_ _/)
      (='.'=)
      (")_(")

    3. #153
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Firstly what you're trying there won't work. If you want to compare two strings you need to use strcmp() or something. strcmp() is a function that returns the number of characters that are different between two strings. In your example, you could have written
      if (!strcmp(userinput, "fuck"))
      (don't forget to include string.h/cstring
      and it would have worked as you expected.

      But in order to search a string containing more than one word, you'd probably have to loop through it and isolate the words (looking for characters that are not letters), and then compare each word against the word you want to detect.

      edit: ... Or you can use the built inn find function of the string class, it seems :p
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    4. #154
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by khh View Post
      Firstly what you're trying there won't work. If you want to compare two strings you need to use strcmp() or something. strcmp() is a function that returns the number of characters that are different between two strings. In your example, you could have written
      if (!strcmp(userinput, "fuck"))
      (don't forget to include string.h/cstring
      and it would have worked as you expected.

      But in order to search a string containing more than one word, you'd probably have to loop through it and isolate the words (looking for characters that are not letters), and then compare each word against the word you want to detect.

      edit: ... Or you can use the built inn find function of the string class, it seems :p
      Absolutely not.

      Code:
      using namespace std;
      string in;
      cin >> in;
      if (in == "fuck"){
         cout << "in == fuck" << endl;
      } else if (in.find("fuck") != string::npos){
         cout << "in contains fuck" << endl;
      }
      strcmp is for char strings, it's C, not C++. The std::string class has == operators for both other strings and char strings.
      Last edited by ninja9578; 01-29-2010 at 04:47 PM.

    5. #155
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Quote Originally Posted by ninja9578 View Post
      strcmp is for char strings, it's C, not C++. The std::string class has == operators for both other strings and char strings.
      Well, it's impossible to tell from his post whether he's asking about C or C++ :p
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    6. #156
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Slash,
      rather than asking little questions here
      you may be better off looking stuff up in a reference guide
      (it'll be quicker, and you'll learn more)

      Eg.
      Here's the reference for the string class
      http://www.cplusplus.com/reference/string/string/

      The methods are at the end
      (\_ _/)
      (='.'=)
      (")_(")

    7. #157
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Quote Originally Posted by khh View Post
      Well, it's impossible to tell from his post whether he's asking about C or C++ :p
      C++, he's using cout

    8. #158
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Thanks, guys.

      And Ynot, I never noticed that on the site, that will be very handy. Thanks.

    9. #159
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Quote Originally Posted by ninja9578 View Post
      C++, he's using cout
      Ah. Busted.
      Oh well, I guess I should only answer questions I'm sure about. Though luckily there's always someone here to inform me when I'm wrong :p
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    10. #160
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      lol, no don't do that. If I correct you then you learn more

    11. #161
      Ex Tech Admin Achievements:
      Created Dream Journal Tagger First Class Veteran First Class 10000 Hall Points Populated Wall Referrer Gold Made lots of Friends on DV
      slash112's Avatar
      Join Date
      Nov 2008
      Gender
      Location
      Sunny Scotland
      Posts
      5,113
      Likes
      1567
      DJ Entries
      29
      Well. I would like to know how Guitarboy is coming along.

      So, Guitarboy? Still at it?

    12. #162
      .. / .- –– / .- .-. guitarboy's Avatar
      Join Date
      Sep 2008
      LD Count
      Over 9000
      Gender
      Location
      Homeward Bound
      Posts
      1,571
      Likes
      49
      Err, not very good at it.
      Trying, but failing.

    13. #163
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Aww, oh noes What are you stuck on?

    14. #164
      .. / .- –– / .- .-. guitarboy's Avatar
      Join Date
      Sep 2008
      LD Count
      Over 9000
      Gender
      Location
      Homeward Bound
      Posts
      1,571
      Likes
      49
      I'm not reall quite sure what I should be doing/learning. I'm going to take classes soon (a month, probably) so I'm going to wait until then.

    15. #165
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      Quote Originally Posted by guitarboy View Post
      I'm not reall quite sure what I should be doing/learning. I'm going to take classes soon (a month, probably) so I'm going to wait until then.
      You know, the first few problems on http://projecteuler.net/ are actually really easy, and perfect for you to practice if you don't really have anything to practice with.

    Page 7 of 7 FirstFirst ... 5 6 7

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •