Newb question--Why can't I ask for a position in an array using a variable?
I keep on getting an error whenever I try to assign character arrays to a postition in another array using a variable to say what position in the second array I want the first array to be stored in. It is hard to say what I mean in a simple way in English. Below is the code for my program. I only included it so you have some idea what's going on in it; you shouldn't need to understand how all of the embedded for loops work. I would GREATLY apprectiate your help.
Code:
/*Program to take sentence user writes and divide into seperate words and
store each word in an array
Skipped commenting first section because it is pretty obvious what's
going on*/
#include <iostream>
using namespace std;
int words();
char sentence[150];
int main()
{
cout << "Enter any words:" << endl;
cin.getline(sentence, 150);
cout << sentence;
return 0;
}
int words()
{
int wordCount = 0;
char wordGather = 15;
char words[10];
int sentenceLength = strlen(sentence);
for(int c = 0; c < sentenceLength; c++) //Start checking for spaces.
{
if(sentence[c] == ' ') //If a space occurs,
{ //
for(int x = c; x <= 0; x--) //Count down from point in array
{ //sentence where a space occured.
int y = 0; //
y = y + 1; //
if(sentence[x] == ' ') //If a space occurs, stop checking.
{ //
break; //
} //
else //If no spaces occur, assign
{ //identified word's chars to array
//'wordGather'.
wordGather[y] = sentence[x];
char words[wordsNumber] = wordGather; //Assign word
} //collected in 'wordGather' to
//space <wordCount> in array
//'words'.
/* In the else statement closest to the end I get error
'illegal constant expression' dealing with the line
'char words[wordsNumber] = wordGather;' and the error
'pointer/array required' for the line 'wordGather[y] = sentence[x];'
*/
}
wordCount = wordCount + 1;
}
}
}