Searching problem

This is a discussion on Searching problem within the C++ Programming forums, part of the General Programming Boards category; Hi thanks for your reply but is there anyway to do this without the string stream? Part of the code ...

  1. #16
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hi thanks for your reply but is there anyway to do this without the string stream?

    Part of the code I have at the moment is posted below as I am searching for a lot of single words that do not requie strings so I have used an array so the strstr() works.

    Code:
          
     char word[500];
    
    	ifstream file;
    
    	file.open("test.txt"); 
    
    
    	if (file.is_open()) 
    
    	{
          while (file>> word)
    	   {
    			if(strstr(word,"hi") != 0)
    			{
    				hi++;
    			}

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Hi thanks for your reply but is there anyway to do this without the string stream?
    Sure just remove the outer loop with the getline in my previous code, and just read directly into word. I modified the previous code to give you an idea. I left it using strings. If you prefer char arrays, just change the following code to make word a char array, and then change the comparison to use the strcmp() function:
    Code:
    if (strcmp(word,"hi") == 0)
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {   
       string filename = "test.txt";
       ifstream file;
       file.open(filename.c_str());
       if (!file.is_open())
       {
          cout << "Unable to open file " filename << endl;
          cin.get();
          return 1;
       }
       
       bool found_hi = false;
       string word;
    
       while (file >> word)
       {
          cout << word << endl;
          if (found_hi == true)
          {
             if (word == "kim")
             {
                cout << "Found hi and kim." << endl;
             }
          }
          else if (word == "hi")
          {
             found_hi = true;
          }
       }
    }

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Part of the code I have at the moment is posted below as I am searching for a lot of single words that do not requie strings
    Why would those words not require a string? In addition, a string type is easier to use than a char array.

  4. #19
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Thanks for the reply, I have two questions:

    I have the problem that when a string is used that the strstr element of my code no longer works. I can use strcmp but for the code I already have I would like to keep the strstr function is that still possible?

    Will the strcmp and the code posted in the example by swoopy allow me to find the two words i.e. hi and kim just on the same line? So if kim and hi were on two seperate lines the counting variable shouldn't be incremented?
    Last edited by 182; 02-16-2006 at 10:08 PM.

  5. #20
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    I would like to see how to. I get what they did up above I mean how would you getline(bla bla) then use the string find(); half. I try looking up how to use find and I tryd some code from what I have seen, but nothing I can/work right. If maybe someone could make a small exmp. on how to use the string find() funiction?
    Last edited by adr; 02-17-2006 at 10:37 PM.

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    The find() function returns the index position where the match was found in the string. If no match is found, then find() returns the constant string::npos, which is a massively large integer.
    Code:
    #include <iostream>
    #include <string>  //string::npos
    using namespace std;
    
    int main( )
    {
    	string str1 = "Hi my name is Kim.";
    	
    	if(str1.find("Hi my") == string::npos)
    	{
    		cout<<"Did not find 'Hi my'."<<endl;
    	}
    	else
    	{
    		cout<<"Found 'Hi my' in the string."<<endl;
    	}
    
    
    	string str2 = "Hi, Kim Jim my friend they are.";
    	
    	if(  (str2.find("Hi") != string::npos) && 
    	     (str2.find("my") != string::npos)  ) 
    	{
    		cout<<"Found both 'Hi' and 'my' in the string."<<endl;
    	}
    	else
    	{
    		cout<<"Didn't find both 'Hi' and 'my'."<<endl;
    	}
    
    
    	string str3 = "Kim is my friend and she said hi to Jim.";
    	
    	int position = str3.find("hi");
    	if(position == string::npos)
    	{
    		cout<<"Didn't find 'hi'."<<endl;
    	}
    	else
    	{
    		if(str3.find("my", position) != string::npos)
    		{
    			cout<<"Found 'hi' and then 'my'."<<endl;
    		}
    		else
    		{
    			cout<<"Didn't find a 'my' after finding 'hi'."<<endl;
    		}
    	}
    
    
    	return 0;
    }
    The last version of find():

    find(string searchTerm, int start)

    starts searching the string at index position start.
    Last edited by 7stud; 02-18-2006 at 12:31 AM.

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I have the problem that when a string is used that the strstr element of my code no longer works. I can use strcmp but for the code I already have I would like to keep the strstr function is that still possible?
    All the functions like strstr(), strlen(), strcmp(), strcat(), i.e. all the ones starting with 'str, only work on char arrays(and the char arrays have to end in a '\0'). Those functions do not work on string types. However, string types have their own comparable functions, which are usually more intuitive. You can use '==' to compare string types. You can get the length of a string type using myString.length(). You can add string types using '+'. And you can search string types using find().

    But, the most flexible aspect of using string types is that they you can store any length string in a string type. With char arrrays, you cannot store a string that is longer than the size of the char array. Also, when you add char arrays together, you have to be sure the destination array is large enough to hold both strings.
    Last edited by 7stud; 02-18-2006 at 11:32 AM.

  8. #23
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hey guys how come the following code doesn't work? Thanks.

    Code:
     char word[500];
    
    	ifstream file;
    
    	file.open("test.txt"); 
    
    
    	if (file.is_open()) 
    
    	{
          while (file>> word)
    	   {
    		if(strstr(word,"hi") != 0)
    		 {  
    	           count1++
    
    			if(strstr(word,"kim") != 0)
    			  {
    				Count2++;
    				
    	                 }	  
    			  
    		 }
              }

  9. #24
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Your input file doesn't exist?
    Your file is empty?
    Your file doesn't contain the words "hi" or "kim"?
    count1 was never initialized?
    Count2 was never initialized?
    Count2 is mispelled?
    You didn't include the <fstream> header file?
    You didn't include the <cstring> header file?
    There's no main() function?
    You never compiled your program before trying to run it?
    You forgot to turn your computer on?
    Last edited by 7stud; 02-18-2006 at 11:41 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Problem with destructors.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 06-11-2004, 12:30 PM
  4. Replies: 5
    Last Post: 12-03-2003, 04:47 PM

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