this is a test question for my class so I've tried to keep it specifically like this:

I need to make a function called:
countWord(String p, String w)
the first parameter 'p' is to hold the paragraph
the second 'w' is to hold the word you want counted
the function is suppose to return a integer value of 0 or more depending on how often your word is found in the paragraph

this is what i have:

int countWord(String &p, String &w)
{
     int counter, spacePos, nextspacePos = 0;
     for (int i = 0; i < p.length(); i++)
     {
          spacePos = (p.substr( spacePos, p.length() ), ' ');
          nextspacePos = (p.substr(spacePos, p.length()), ' ');
          if (w == p.substr(spacePos + 1, w.length()))
          {
                 counter++;
                 i = nextspacePos;
          }
      }
      return(counter);
}

You can't use a single >> to enter in more than one word. The space between words is white space and marks the end of your "paragraph" p. To have the user enter more than one word, you'll need to use some type of loop or you'll need to use getline.

I noticed i really screwed up the phrasing of this topic so here i will do it again

this is a question on a test in my class so it has to be specifically like this:
well I've tried to compile it and I've come across an 2 errors regarding the p.find i have
in my for loop ---- error C2661: 'find' : no overloaded function takes 2 parameters
A friend somewhat made the loop for me but i really need more help understanding how to make a loop that works sucessfully

countWord(String p, String w)
the first parameter p is to hold the paragraph
the second is to hold the word you want counted
the function is suppose to return a integer value of 0 or more depending on how often your word is found in the paragraph

#include <iostream.h>
#include <lvp\string.h>

int countWord(String &p, String &w)
{
  int counter, spacePos, nextspacePos = 0;
  for (int i = 0; i < p.length(); i++)
  {
    spacePos = p.find(p.substr(spacePos, p.length() ), ' ');
    nextspacePos = p.find(p.substr(spacePos, p.length()), ' ');
    if (w == p.substr(spacePos + 1, w.length()))
    {
      counter++;
      i = nextspacePos;
    }
  }
  return(counter);
}

int main()
{
  String p;
  String w;

  cout << "Type in a paragrah below" << endl;
  getline (cin, p);
  cout << "Type in a word you woud like to have counted: ";
  cin >> w;

  cout << countWord(p, w);
  return(0);
}

I noticed i really screwed up the phrasing of this topic so here i will do it again

this is a question on a test in my class so it has to be specifically like this:
well I've tried to compile it and I've come across an 2 errors regarding the p.find i have
in my for loop ---- error C2661: 'find' : no overloaded function takes 2 parameters
A friend somewhat made the loop for me but i really need more help understanding how to make a loop that works sucessfully

countWord(String p, String w)
the first parameter p is to hold the paragraph
the second is to hold the word you want counted
the function is suppose to return a integer value of 0 or more depending on how often your word is found in the paragraph

#include <iostream.h>
#include <lvp\string.h>

int countWord(String &p, String &w)
{
int counter, spacePos, nextspacePos = 0;
for (int i = 0; i < p.length(); i++)
{
spacePos = p.find(p.substr(spacePos, p.length() ), ' ');
nextspacePos = p.find(p.substr(spacePos, p.length()), ' ');
if (w == p.substr(spacePos + 1, w.length()))
{
counter++;
i = nextspacePos;
}
}
return(counter);
}

int main()
{
String p;
String w;

cout << "Type in a paragrah below" << endl;
getline (cin, p);
cout << "Type in a word you woud like to have counted: ";
cin >> w;

cout << countWord(p, w);
return(0);
}

Not sure what compiler you are using, but when I change this:

#include <iostream.h>
#include <lvp\string.h>

to this:

#include <iostream>
#include <string>

and add this:

using namespace std;

it compiles for me.


[EDIT]
Looking at it more closely, is this what you want?

spacePos = p.find(p.substr(spacePos, p.length() ), ' ');

I think that's going to take the ASCII value of a space (32) and skip the first 32 characters of the p string, which I doubt is what you want.
http://www.cplusplus.com/reference/string/string/find.html
[/EDIT]

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.