Adding strings to a struct (Help)

I'm really new to C++ so I'm sorry in advance. So I think there's something I'm not completely understanding. I'm trying to take in a sentence and add each word to a structure in order to translate it to pig latin. I'm not sure where I've gone wrong. When I try to print the struct Words[index].english I just get a space.

struct Word
{
string english;
string piglatin;
};

void calculateWords(string sentence)
{
int count = 1;
int size;
int location = 0;
int charCount = 0;
int newStart = 0;
int a = 20;
size = sentence.length();

for( int i = size+1; i != 0; i--)
{
if(sentence[i] == ' ')
{
sentence.pop_back();
count++;
}

}
cout<<count<<endl;

Word Words[20];

for(int j = 0; j < size; j++)//Go over the entire sentence
{
if(sentence[j] == ' ')//find the spaces
{
location = j-1;//find the end of the word
charCount = (location - newStart)+1;//calc how many chars
for( int k =0; k < count; k++)//Enter it as one of the words
{
//copy the word to they array
Words[k].english = sentence.substr(location,charCount);
}
newStart = j + 1;
}



}

cout<<Words[0].english<<endl;
}
I would look over this part of your code.

1
2
3
4
5
6
7
8
for (int i = size + 1; i != 0; i--)
{
	if (sentence[i] == ' ')
	{
		sentence.pop_back();
		count++;
	}
}


Topic archived. No new replies allowed.