I've been trying to get rid of this error for the past couple of hours, if anyone could help me get rid of it that would be great. Thanks!
When trying to compile it returns
compiling error 17: error: expected initializer before 'void'
usingnamespace std;
void WordCountInLine(string line, int& WordCount)
void NumberOfBlanks(string line, int& NumofBlanks, int& NonnumBlank)
void WordCountInLine(string line, WordCount);
// This function counts the words in a line of text.
{
string delimiters = " .,?:;!()";
int charCount;
char currentChar;
// Add delimiter at the end of the line for word identification.
line = line + '?';
// Initialize word count as zero.
WordCount = 0;
// Initialize a flag that indicates we start without being in a word.
bool InsideWord = false;
// For each character in line
for ( charCount = 0; charCount<line.length(); charCount++ )
{
// Set current chatacter
currentChar = line.at(charCount);
// If it is not a delimiter
if ( delimiters.find( currentChar ) == string::npos )
{
// If in a word
if (InsideWord)
// Increment current word length
WordCount++;
// else (not in a word)
else
{
// Starting a word - Change in word flag
InsideWord = true;
// Start keeping word count.
WordCount = 1;
}
}
// else character is a delimiter
else
{
// If in a word.
if (InsideWord)
{
// You're reaching the end of the word. (marked by delimiter) reset flag for inword
InsideWord = false;
// else not in a word.
// Do nothing, the computer is in between words.
}
}
}
}
void NumberOfBlanks(string line, NumofBlanks, NonnumBlank);
// This function will count the number of blanks and the number
// of non-blank characters in a string of text.
{
//Initialize blanks and non blanks to zero.
NumBlanks = 0;
NonBlank = 0;
// For each character in line
for ( charCount=0; charCount<line.length(); charCount++ )
{
// Set current character
currentChar = line.at(charCount);
// If current character is a blank
if (currentChar == ' ');
{
NumBlanks = NumBlanks + 1;
}
//else current character is a non blank character.
else
{
NonBlank = NonBlank + 1;
}
}
}
int main()
{
7: error: expected initializer before 'void'
19: error: expected unqualified-id before '{' token
There are no semicolons for your prototypes, but you don't need prototypes since your functions are declared before main starts anyways so just remove the first two lines after usingnamespace std;
For those reading this later, the code above should begin
1 2 3 4 5 6 7 8 9 10
usingnamespace std;
void WordCountInLine(string line, int& WordCount); // ; was missing
void NumberOfBlanks(string line, int& NumofBlanks, int& NonnumBlank); // ; was missing
void WordCountInLine(string line, int& WordCount) // int& was missing, ; was unwanted
// This function counts the words in a line of text.
{
...
The error on line 7 was due to the missing ; on the previous statement.
The error on line 19 was because the ref param was incompletely declared (no type)