Minjae Minjae - 1 year ago 110
C++ Question

C++ String as an array in function, keeps getting an error message

I have an assignment to create a simple string reader that asks user to enter a sentence, and fix the followings:

-first, using function prototype of

int splitSent(string sentence, string words[], int maxWords);

where the function returns number of words in sentence, and 'maxWords'

-if user's input starts with small letter, caplitalize it.

-if user input contains the string, 'Computer Science', switch the strings to 'CS'

For the first task, I'm being forced NOT to use dynamic arrays, vectors, and any 'char' variable. Also, I must keep the given prototype.

I could definitely do it in main function using a single for-loop to figure out how many words are there, but the given task is driving me crazy with errors since I've never used string as array in function call, without using any char variable, vectors an so on.

In the code below I wrote, in the main function, do I have to calculate all the values in the main function before the function call to get it work? For now, it gives an error message saying

In function 'int main()':
[Error] expected primary-expression before ']' token
At global scope:
[Error] declaration of 'words' as array of references
[Error] expected ')' before ',' token
[Error] expected unqualified-id before 'int'

I got stuck here for many hours and there is nothing more I can get from googling anymore. Please advise.

#include <iostream>
#include <string>

using namespace std;

int splitSent(string sentence, string words[],int maxWords);

int main (){
string sentence,words;
int maxWords;
cout << "Enter a sentence. (Maximum words allowed - 100)" << endl;
getline(cin, sentence);

splitSent(sentence, words[],maxWords);

return 0;


int splitSent(string sentence, string& words[],int& maxWords)
int temp=0, count=1;
for (int j=0; j<sentence.length(); j++) if (sentence[i]==' ') count++;

maxWords == count;

for(int i=0; i<sentence.length(); i++) {
if (sentence[i] == ' ') temp++;
else if (sentence[i] != ' ') words[temp] += sentence[i];}

return (count);

Answer Source

There were some syntax errors and some logic problems.

I noticed you tried to pass words[] into the function. It can be confusing when to use words and when to use words[]. When you are declaring it, use string words[], eg in the function definition's parameter list. This is to tell the function what to expect. When you pass it in, use words, because you're passing the entire thing. Inside the function, use words[index] because you're referring to a specific element of the array.

You have to declare words as an array of string, not as a string.

You just pass the string array in directly, not by reference. When you pass it directly, you're not passing a copy of the array, the compiler ensures the function has the address of the first element in the array, so there is no performance penalty and you are working on the original array - which is what you want.

Your function declaration at the top is different to the function definition further down. They should both be the same - the declaration is the version you should use.

int splitSent(string sentence, string words[],int maxWords);

You wanted maxWords by reference but this shouldn't be modified in the function because it holds the size of the array. It stops you from running past the end of the array.

Function splitSent ignores multiple spaces. It reads maxWords but doesn't update it. It returns the actual number of words it finds. This may be greater than maxWords but the words array is only written up to a length of maxWords.

// returns number of words in sentence
// updates words[], using `maxWords` to prevent writing past end of array

int splitSent(string sentence, string words[], int maxWords)
    int word_index = 0;
    int letter_count = 0;

    //words[count];  // not sure what was intended by this statement

    for (int i = 0; i<sentence.length(); i++) {
        if (sentence[i] == ' ') {
            // handles multiple spaces in a row
            if (letter_count > 0) {
                letter_count = 0;
        else if (word_index < maxWords) {       // check the word_index is within bounds of array
            words[word_index] += sentence[i];
    if (letter_count > 0)
        return (word_index + 1);
    return (word_index);

int main( ) {
    string sentence;
    int const maxWords = 100;   // constant integer, so compiler lets us use it to declare the size of the array
    string words[maxWords];     // function expects an array of strings, so have to declare one
    cout << "Enter a sentence. (Maximum words allowed - " << maxWords << ")" << endl;
    getline(cin, sentence);
    int wordCount = splitSent(sentence, words, maxWords);
    return 0;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download