-2

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++;

    words[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);
}
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
Minjae
  • 31
  • 1
  • 2
  • 8

1 Answers1

0

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) {
                word_index++;
                letter_count = 0;
            }
        }
        else if (word_index < maxWords) {       // check the word_index is within bounds of array
            words[word_index] += sentence[i];
            letter_count++;
        }
    }
    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;
}
John D
  • 1,627
  • 1
  • 11
  • 10
  • I've been confusing with arrays' syntax in function call. Thanks a lot! I just couldn't resist having words[] in function prototype. – Minjae Oct 27 '16 at 02:10
  • Can you explain a bit more about the reason why you've declared maxWords as const variable? It's not going to be changed even after the function call ,and it works okay without const. – Minjae Oct 27 '16 at 02:11
  • There are 2 reasons. First is that you can only declare an array with a constant size so `string words[maxWords];` is only allowed if `maxWords` is declared as `const`. Second is that when you assume something is constant, it's good to get the compiler to check you're not changing it anywhere by mistake. By declaring it `const`, the compiler will show you an error if it finds you writing to it anywhere. It's not so hard to check here, but in much larger programs, it can be very difficult to manually find bugs caused by modifying a variable you are relying on to be a constant. – John D Oct 27 '16 at 02:21
  • Thank you so much. I really appreciate your help. – Minjae Oct 27 '16 at 02:37