简体   繁体   English

function 中的 while 循环上的 ')' 标记错误之前的预期主表达式

[英]expected primary-expression before ‘)’ token error on a while loop in a function

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);"我的问题是,在 NumWords function 的 while 循环中,我一直在变量“string”上收到“expected primary-expression before ')' token”错误。该变量在“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 //主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";

}

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.每个单词由两个整数 n1 和 n2 定义,它们固定单词的开头和结尾。 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.这一段是使用 string::substring(n1, n2-n1) 和 push_back 提取到向量中的。

#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();
  }

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM