everything seems to go fine except when i get to the search. either the program runs with a runtime error or it'll just return "NO MATCH". by the way, when i put the if/else statement in the loop, it works but it also returns NO MATCH until it finds the characters.

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstring>
#include<string>
 
using namespace std;
 
int main()
{
 ifstream inFile;
 char data[20][30];
 char search[30];
 int incVar;
 
 inFile.open("C:/Documents and Settings/user/My Documents/CSE1384/lab5/input.txt");
 for(int k=0; k<10; k++)
 {
  inFile.getline(data[k], 30);
  cout << data[k] << endl;
 }
 cout << endl;
 
 
 char *strPtr;
 cout << "Enter for a phrase to search: ";
 cin.get(search, 30);
 cout << "Your search phrase is: " << search << endl;
 cout << endl;
 cout << endl;
 cout << "Search results: " << endl;
 incVar = 0;
 for(int i=0; i<20; i++)
 {
 strPtr = strstr(data[i], search);
 
 if(strPtr==0)
   cout << "NO MATCH" << endl;
 else
 {
  cout << data[i] << endl;
  incVar++;
 }
 
 }
 
 
 cout << endl;
 cout << "Records found: " << incVar << endl;
 cout << endl;
 
 inFile.close();
 
 return 0;
}

Recommended Answers

All 8 Replies

It would be really good if u could post the data file so that we can run the prog and see wat actually is the problem rather than assuming somethings.

Also if u are using C++ y not use string instead of character arrays.

Bye.

sorry about that. here's the input file.

Joe Smith, 555-324-5532
Mike Newman, 234-234-3686
Jennifer Meadow, 322-342-1252
Jonathan Peters, 383-123-4135
Laura Marino, 123-345-2341
Hanna Petersons, 678-123-3631
Jordan Gwent, 356-234-4572
Susan McKee, 636-567-1230
Lisa McDonnell, 243-635-1235
Oliver Fedor, 982-234-1517

and i tried string already. same error.

post your code using std::string because it does work with that. replace strstr() with the std::string's find() method, like this

for(int i=0; i<20; i++)
 {
	 if( data[i].find(search) == string::npos )
		cout << "NO MATCH" << endl;
	else
	{
		cout << data[i] << endl;
		incVar++;
	}
 
 }

post your code using std::string because it does work with that. replace strstr() with the std::string's find() method, like this

for(int i=0; i<20; i++)
 {
     if( data[i].find(search) == string::npos )
        cout << "NO MATCH" << endl;
    else
    {
        cout << data[i] << endl;
        incVar++;
    }
 
 }

i get an error that says "left of

.find

must have class/struct/union...?

From the original post, I changed this:

for ( int i=0; i<20; i++ )
   {
      if ( strstr(data[i], search) )
      {
         cout << data[i] << endl;
         incVar++;
      }
   }

Results:

H:\>"Testpp.exe"
Joe Smith, 555-324-5532
Mike Newman, 234-234-3686
Jennifer Meadow, 322-342-1252
Jonathan Peters, 383-123-4135
Laura Marino, 123-345-2341
Hanna Petersons, 678-123-3631
Jordan Gwent, 356-234-4572
Susan McKee, 636-567-1230
Lisa McDonnell, 243-635-1235
Oliver Fedor, 982-234-1517

Enter for a phrase to search: an
Your search phrase is: an


Search results:
Mike Newman, 234-234-3686
Jonathan Peters, 383-123-4135
Hanna Petersons, 678-123-3631
Jordan Gwent, 356-234-4572
Susan McKee, 636-567-1230

Records found: 5


H:\>"Testpp.exe"
Joe Smith, 555-324-5532
Mike Newman, 234-234-3686
Jennifer Meadow, 322-342-1252
Jonathan Peters, 383-123-4135
Laura Marino, 123-345-2341
Hanna Petersons, 678-123-3631
Jordan Gwent, 356-234-4572
Susan McKee, 636-567-1230
Lisa McDonnell, 243-635-1235
Oliver Fedor, 982-234-1517

Enter for a phrase to search: Dave
Your search phrase is: Dave


Search results:

Records found: 0

wow that seemed to have worked hehe. thank you so much i appreciate it!

for the cout statement, i just made an if statement that would output "no match" if the incVar==0. thanks for the help all around :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.