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