0

My issue is that I keep getting the "expected primary-expression before ')' token" error on the variable "string" in the while loop in the NumWords function. The variable works fine in the "istringstream inSS(string);" line but when I try to compile the code the next line produces that error. Please help I am so confused and it is driving me crazy.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

//Function prototypes

int NumWords(const string&);

int NumNonWSCharacters(const string&);

void CharReplace(string&, char, char);

char PrintMenu();

//Main function

int main () {

//Variables
string text;

//Input & Output original
cout << "Enter a line of text: ";
getline(cin, text);
cout << "\n";
cout << "You entered: " << text << "\n";

//How many words
cout << NumWords(text);

}

//Counts the number of words in a string

int NumWords(const string&) {
int count = 0;
istringstream inSS(string);
while (inSS >> string) {

    count++;

}


}

//Count the number of characters (not including whitespace) in a string

int NumNonWSCharacters(const string&) {

    cout << "FINISH\n";

}

//Replaces one character with another in a given string

void CharReplace(string&, char, char) {

    cout << "FINISH\n";

}

//Prints the menu

char PrintMenu() {

    cout << "FINISH\n";

}
  • 4
    You're not naming your parameters. `string&` is the type of the parameter. I recommend a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 13 '20 at 12:06

1 Answers1

0

For your reference. Good luck.

I save the words in an array words, vector of strings. The multiple space or noncharacter letters are all excluded. Each word are define by two integers n1 and n2, which pins the beginning and the end of a word. A word is defined as a piece of continuous character. This piece is extracted using string::substring(n1, n2-n1), and push_back to the vector.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
typedef std::vector<std::string> WordArray;
int NumWords(const std::string&, WordArray&);
int NumNonWSCharacters(const std::string&);
int main ()
{
   std::string text;
   WordArray words;
   //Input & Output original
   std::cout << "Enter a line of text: ";
   std::getline(std::cin, text);
   std::cout << "\n";
   std::cout << "You entered: " << text << "\n";
   //How many words
   std::cout << "Number of characters = " <<NumNonWSCharacters(text) << std::endl;
   int nword = NumWords(text, words);
   std::cout << "Number of words = " << nword << std::endl <<std::endl;
   for (int i=0; i<words.size(); i++) std::cout <<"word[" << i <<"] = " << words[i] << std::endl;
   return 0;
  }
 int NumNonWSCharacters(const std::string& str)
 {
    int count = 0;
    std::istringstream inSS(str);
    char a;
    while (inSS >> a)
      {
        if (a!=' ')  count++;
      }
    return count;
 }
#include <cctype>
int NumWords(const std::string& str, WordArray&word)
{
    int nword = 0, n1, n2;
    char a;
    n2 = n1 = 0;
    while (n1 < str.size() )
     {
       if ( std::isalpha(str[n1]) )
         {
           n2 = n1 + 1;
           while ( std::isalpha(str[n2]) ) {n2++;}
           word.push_back(str.substr(n1, n2-n1));
           n1 = n2+1;
         }
       else
        {
          n1++;
          while ( !std::isalpha(str[n1]) ){n1++;}
         }
      }
    return word.size();
  }

enter image description here

ytlu
  • 412
  • 4
  • 9
  • This counts the number of letters, but what about counting the number of words? – Jackieboy Nov 14 '20 at 10:01
  • @Jackieboy You may change the test "isalpha" ( is it a alphabetical char?) to "isalnum" (is it alphabetical or number), if you want to include the number 0-9 as part of a word. – ytlu Nov 14 '20 at 15:34