Welcome to Dream.In.Code
Become a C++ Expert!

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!




occurrance error

 
Reply to this topicStart new topic

occurrance error

dms02x
16 Mar, 2008 - 07:50 AM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 18

can u help me map out the error, cant run.
help me map out the error. tytyty

CODE

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

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*/
        }

getch();
return 0;
}
crazy.gif
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Occurrance Error
16 Mar, 2008 - 08:02 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,998



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Before we can help we need to know the exact error message you're receiving smile.gif
User is online!Profile CardPM
+Quote Post

dms02x
RE: Occurrance Error
16 Mar, 2008 - 08:07 AM
Post #3

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 18

im know im not good at programing. plz help me out..
this programs basicly outputs the number of occurance..



char *strstri(char *t, char *s)
{



a function-definition is not allowed here before '{' token
expected ',' or ',' before '{' token
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Occurrance Error
16 Mar, 2008 - 08:25 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,998



Thanked: 126 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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

cpp

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

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;
}

User is online!Profile CardPM
+Quote Post

Martyr2
RE: Occurrance Error
16 Mar, 2008 - 10:50 AM
Post #5

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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.

cpp

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
using namespace std;

char *strstri(char *t, char *s);

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.

Enjoy the example and hope it helps.

"At DIC we be substring hunting code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month