Place Words from String into Array

Hello! I am trying to get only the words of a string to store into an array. For example, the string text == "cat rat Shark mouse" should cause the array newtext[4] to hold "cat, rat, Shark, mouse".

This code will not build and I cannot find the mistake I made. Thanks for any help!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <cassert>
#include <fstream>

using namespace std;


int words(string text, string newtext[])
{
	for (int k = 0; k < text.size() -1; k++) 
	{
		int n = 0;
		if (text[k] != ' ')
		{
			for (int j = 1; j < text.size() - k - 1; j++)
			{
				if (text[k + j] == ' ')
				{
					newtext[n] = text.substr(k, j - 1); k = (k + j - 1); n + 1; break;
				}
				j++;
			}

		}
	}
}

int main()
{
	string text;
	string newtext[500];
	getline(cin, text);
	words(text, newtext);
	cout << newtext;
	
}
error C4716: 'words' : must return a value


Either you return a value or change words() to void.
Whoops! Changed it to void. thanks man! How do you get it to show what the error was? Mine just says there were build errors. (microsoft visual studios c++)
It depend what version of Visual Studio you use.
Have a look for View->ErrorList or View-OtherWindows->ErrorList
Topic archived. No new replies allowed.