Join 136,912 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,737 people online right now. Registration is fast and FREE... Join Now!
int main () { string line; int i = 0; long l,m; float perc; char *location; char string1[80], string2[80]; int appearances = 1; ifstream myfile ("input.txt");
if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line,'\t'); cout <<line<<endl; l = myfile.tellg(); myfile.seekg (0, ios::end); m = myfile.tellg();
}
if ( myfile == NULL ) printf("No match was found.\n"); else cout<<" was found at position", string2, location-string1; }
char *strstri(char *t, char *s) {
int i, j; for(i=0; t[i] != '\0'; i++)
{ for(j=0; s[j] != '\0'; j++)
{ if ( toupper(s[j])==toupper(t[i+j]) ) continue; else break; } /*if the whole string was successfully compared, break the loop*/ if (s[j] == '\0') break; } if (s[j] == '\0') return (i+t); /*returns a pointer to the first occurrence of s within t, just like the original strstr*/ else getch(); return (0); /*return NULL*/ }
The way your code is now you're trying to declare a method inside the Main() method, which isn't allowed. Also, some of your brackets { } are reversed, you have a } where you need a {. Take a look at the modifications I made and see if this gets rid of your error
int main () { string line; int i = 0; long l,m; float perc; char *location; char string1[80], string2[80]; int appearances = 1; ifstream myfile ("input.txt");
if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line,'\t'); cout <<line<<endl; l = myfile.tellg(); myfile.seekg (0, ios::end); m = myfile.tellg();
}
if ( myfile == NULL ) printf("No match was found.\n"); else cout<<" was found at position", string2, location-string1; } } //<---- you were missing this closing bracket
char *strstri(char *t, char *s) {
int i, j; for(i=0; t[i] != '\0'; i++) { for(j=0; s[j] != '\0'; j++) { if ( toupper(s[j])==toupper(t[i+j]) ) continue; else break; } /*if the whole string was successfully compared, break the loop*/ if (s[j] == '\0') break; { if (s[j] == '\0') return (i+t); /*returns a pointer to the first occurrence of s within t, just like the original strstr*/ } else { getch(); return (0); /*return NULL*/ } } //Im not sure if you wanted this inside the loop //or outside it getch(); return 0; }
Besides what Psycho pointed out, you had lots of problems with this in your actual search function. I have made a few rewrites to it to show you one way this can be done.
int main () { string line; int i = 0; //long l,m; //float perc; char *location; char string1[80], string2[80]; int appearances = 1;
ifstream myfile ("input.txt");
if (myfile.is_open()) { while (! myfile.eof() ) { getline(myfile,line,'\n'); cout << line <<endl;
// Convert our string to a c-string and dump // it into string1 strcpy(string1,line.c_str());
// Ok so lets search for "needle" in our string1 location = strstri("needle",string1);
// If it is not null, we found it, spit out // the match along with the rest of the line // (after all it is a pointer into the string) if (location != NULL) { cout << "Substring: " << location << endl; } } }
getch(); return 0; }
char *strstri(char *t, char *s) { int i, j; bool found = false;
for(j=0; s[j] != '\0'; j++) { // First find if character in needle is same as haystack if (s[j] == t[0]) {
// If first letter found, run through the rest of the // needle word. for(i=0; t[i] != '\0'; i++) { // While characters match and we are not at the end // of the haystack string, keep found true if ((s[j+i] != '\0') && (s[j+i] == t[i])) { found = true; } else { found = false; break; } }
// If found is still true here, it means that the entire // needle string was found in the haystack string. // So return a char pointer to the substr start if (found) { char *charptr = &s[j]; return charptr; } } }
// We return null if the string was not found. return NULL; /*return NULL*/ }
Read through the in-code comments to see how this works. Essentially what we do is go through the target string (aka haystack) and char by char see if it matches the first char of our "needle" word. Once we find a match there, we go into a loop which checks the following letters against the needle letters and if all match then our boolean value is going to be true. Then all we need to do is return the point in the haystack where we branched into the needle check.