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";
}
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.