This is still a work in progress, but my question is this-> I am trying to make a string array that stores each char in a sentence until a whitespace or \0. Currently, it seems like its creating an new element of the array for every letter(?). Any suggestions on how to fix this code.
(ignore the bits about piglatin, havent gotten to that yet)
// stores individual word and its piglatin translation
struct Word{
string english;
string piglatin;
};
int main()
{
Word wordArr[75] {};
char chars[250] {'\0'};
// ask user to enter a string using getline
cout << "Please enter a sentence to be converted into piglatin: ";
cin.getline (chars, 250);
// calculate how many words are in string and make "Word" array of same size
int wordCount = 0;
for (int i = 0; chars[i] != '\0'; i++)
{
int wordSize = 0;
// convert to lowercase
if (chars[i] != ' ')
{
tolower(chars[i]);
wordSize++;
}
// make string of english word
string tempString(chars, i, wordSize);
wordArr[wordCount].english = tempString;
cout << wordArr[wordCount].english;
cout << wordCount;
wordSize = 0;
}
// store the separate words into structure Word
// strcat() <- adds ay to end
return 0;
}
because tolower only returns the converted character, it doesn't write to the passed argument.
Also "wordCount" never advances, it stays at 0, over-writing the first/0-th element again and again. You prolly meant add "++wordCount" at the end of your for-loop. You should also subsequently add a check for wordCount reaching or exceeding the size of your word array (ie, 75), and breaking the loop if so. Just to be sure you don't do an out-of-bounds access.
Your string constructor on line 36 is not right either. I would replace lines 36 & 37 with: