Im working on this program right now and it is supposed to count the words in a string and it is intelligent enough to not count puncuations as a word. My first funtion works, but i am having problems with my second. The second function gets a word number from the user and displays the correct word. For example, Hello my name is Amir. I want word 4 so i will display "is". Can anyone kindly help me thanks!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int wordCount(string inputedText);
int getWord(int num, string selectedText);
int main()
{
int selectionNumber;
string selectionText;
string userText = "Power Overwhelming!";
cout<<userText<<endl;
cout<<"The number of words in the string is: "<<wordCount(userText)<<endl;
cout<<getWord(int num, string selectedText)<<endl;
system("PAUSE");
return 0;
}
int wordCount(string inputedText)
{
int wordCounter = 0;
unsigned int stringLength;
unsigned int n;
string delimiters = " !?,.;";
stringLength = inputedText.length();
for(n=0; n < stringLength; n++)
{
if(delimiters.find(inputedText[n]) != string::npos && n != 0)
{
if(delimiters.find(inputedText[n-1]) == string::npos)
{
++wordCounter;
}
}
if((n == stringLength-1)&&(delimiters.find(inputedText[n]) == string::npos))
{
++wordCounter;
}
}
return wordCounter;
}
int getWord(int num, string selectedText)
{
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int wordCount(string inputedText);
int getWord(int num, string selectedText);
int main()
{
int selectionNumber;
string selectionText;
string userText = "Power Overwhelming!";
cout<<userText<<endl;
cout<<"The number of words in the string is: "<<wordCount(userText)<<endl;
cout<<getWord(int num, string selectedText)<<endl;
system("PAUSE");
return 0;
}
int wordCount(string inputedText)
{
int wordCounter = 0;
unsigned int stringLength;
unsigned int n;
string delimiters = " !?,.;";
stringLength = inputedText.length();
for(n=0; n < stringLength; n++)
{
if(delimiters.find(inputedText[n]) != string::npos && n != 0)
{
if(delimiters.find(inputedText[n-1]) == string::npos)
{
++wordCounter;
}
}
if((n == stringLength-1)&&(delimiters.find(inputedText[n]) == string::npos))
{
++wordCounter;
}
}
return wordCounter;
}
int getWord(int num, string selectedText)
{