question about strings

This is a discussion on question about strings within the C++ Programming forums, part of the General Programming Boards category; im relatively new to C++, but have a general idea of the basics. Anyway, what ive been trying to do ...

  1. #1
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18

    question about strings

    im relatively new to C++, but have a general idea of the basics.
    Anyway, what ive been trying to do is make a program that can do a basic form of conversation, by searching for certain keywords in input from the user, however, i am having trouble getting the hang of searching within input for certain keywords.
    i have only just started by getting it to say hello, should a user enter a string containing the word hi.
    The problem that occurs is that the first time, if you enter hi, it would reply, but from then on, if you say hi, it doesnt reply. However, it would reply if you have at least two characters before the word hi. does anyone know why this happens? and if so, how i can solve the problem?
    Here is my program so far:


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string talk;
        int search;
        while(1==1)
        {
                   getline(cin, talk, '\n');
                   search = talk.find("hi",0);
                   if (search >= 0)
                   {          
                              cout<<"hello";
                   }
                   cin.get();
                   cin.get();
        }
    }

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    it's the cin.get()'s you have there... here's what your code should look like:
    Code:
    #include <iostream>
    //#include <fstream>    //used for File I/O - you don't need it here
    #include <string>
    
    using namespace std;
    
    int main()
    {
            string talk;
            int search;
            for(;;) //this is the best way to write an infinite loop
            {
                    getline(cin, talk, '\n');
                    search = talk.find("hi",0);
                    if (search >= 0)
                    {
                            cout<<"hello\n";        //put a newline to seperate
                                                    //input from output
                    }
                    //cin.get();    //what were these for?
                    //cin.get();
            }
            return 0;       //you should return 0 at the end of main
    }
    here's another way it could be done (note the use of strstr in the conditional)
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
            std::string talk;
            for(;;) //this is the best way to write an infinite loop
            {
                    getline(std::cin, talk, '\n');
                    if (strstr(talk.c_str(),"hi"))  //enter this block if "hi" is found in talk
                    {
                            std::cout<<"hello\n";   //put a newline to seperate input from output
                    }
            }
            return 0;       //you should return 0 at the end of main
    }
    Last edited by major_small; 07-16-2005 at 04:04 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18
    the cin.get()'s are a problem?
    hmmm they are there because i put them there to stop the program from closing... ohhhh i see the problem now.
    1) they're not at the end of the program where they should be and
    2) i dont need them at all because there is an infinite loop in the program

    sorry, force of habit

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    You're not using talk.find() correctly, methinks.
    Perhaps something like this:
    Code:
    int main() {
    	for (;;)
    	{
    		string talk;
    		getline(cin, talk);
    		if (talk.find("hi") != string::npos)
    		{
    			cout << "hello\n";
    		}
    	}
    }
    Of course, at the moment your program will respond with "hello" if someone calls it hideous. Oh well.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    mysterious mongoose
    Join Date
    Apr 2005
    Posts
    18
    dont worry, it works now (and has about 30 words and 15 phrases), and im not too worried about the hideous thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 05:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  4. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-14-2005, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21