STRSTR searching

I'm writing a program that searches 5 words in 30 lines to text, but I need to start out small so i'm doing 3 words in 3 line of text first. Right now i'm getting an Overload error when using strstr() ... I don't know if i'm using it right or not. here the code ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
int 

main ()
{
     int a, MAX = 50, b, numberLines; 
     char word1[10] = " " , word2[10] = " ", word3[10] = " ";
     char message1[50], message2[50], message3[50]; 
     char * word; 
     
     
     fstream wordsin("wordsin.txt", ios::in);
     fstream wordsout("wordsout.txt", ios::out | ios::trunc);
     if (!wordsin || !wordsout) 
     {
     cout << "Error: Unable to open input or output files";
     cin.get();
     exit(1);
     }
     
     wordsout << "Enter the first words " ; 
     wordsin >> setw(11) >> word1; 
    
     wordsout << "Enter the second words " ; 
     wordsin >> setw(11) >> word2; 
     
     wordsout << "Enter the third words " ; 
     wordsin >> setw(11) >> word3; 

     wordsout << "Enter the first message " ; 
     wordsin.getline(message1, 50, ' '); 
     
     wordsout << "Enter the second message " ; 
     wordsin.getline(message2, 50); 
 
     wordsout << "Enter the third message " ; 
     wordsin.getline(message3, 50);
     
     wordsout << message1 << message2 << message3; 
     
     word = strstr(message1, word1); 
     wordsout << word; 
     
     for (a = 0; a < MAX; a++)
     {    
     for (b = 0; b <numberLines; b++)
      {  
//            if (strstr(message1[a], word1[a] ) )
            wordsout << b;
      }
     }
 
     wordsin.ignore ();
     wordsin.get ();
     

return 0;
}


If there any other suggestion to make this program better, please let me know or if there a good reference website for this kind of thing ... thanks
You've got some bugs to fix. But first off, what you're passing to strstr is a single char at offset a in the char arrays mesage1 and word1. You should try strstr(message1, word1);

Try this one:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

const unsigned kArraySize = 3;

int main()
{
  std::string words[kArraySize];
  std::string messages[kArraySize];
  for (unsigned i = 0; i < kArraySize; ++i) {
    std::cout << "Enter message #" << i + 1 << ": ";
    getline(std::cin, messages[i]);
    std::cout << "Enter word #" << i + 1 << ": ";
    getline(std::cin, words[i]);
  }
  std::cout << "\nChecking messages for words.\n" << std::endl;
  for (unsigned i = 0; i < kArraySize; ++i) {
    for (unsigned j = 0; j < kArraySize; ++j) {
      if (messages[i].find(words[j]) != std::string::npos)
        std::cout << "Word \"" << words[j] << "\" found in Message: \"" <<
          messages[i] << '"' << std::endl;
    }
  }
  return 0;
}


If you are using the Microsoft tools, you might need std:: in front of getline();

~psault
Topic archived. No new replies allowed.