#include "ccommandparser.h"
#include <iostream>
void Ccommandparser::getCommand()
{
std::cout << "Please enter your decision." << std::endl;
getline(std::cin, sentence);
findWords();
}
void Ccommandparser::findWords()
{
startPos = 0;
endPos = 0;
wordsFound = 0;
for ( unsignedint current = 0; current <= sentence.length(); current++ )
{
endPos = current;
if ( sentence[current] == ' ' || current == sentence.length() ) // Iterate through sentence checking for empty space OR end of string
{
for ( unsignedint copying = startPos; copying < endPos; copying++ ) // Copy chars from string from start position to end position
{
word[wordsFound] += sentence[copying];
}
wordsFound++; // add to words found, queing up next array location for next word
startPos = endPos + 1; // New starting position is one PAST the empty space that was located
}
if ( wordsFound == LIMIT ) // if more than LIMIT words are found, exit function
{
break;
}
}
}
Not had time to look at the logic as such but..
is there ever a chance that "wordsFound" variable will be greater than 10 (i.e. the value of LIMIT)?
Also i'd consider using an stl container like std::vector over a horrible c-style array any day.
Also if startPos, endPos, and wordsFound are only going to be used in findWords() then define them as local variables. Similarly for sentance in getCommand(). (although i can see they would be useful member variables if you are going to expand this class).
Not had time to look at the logic as such but..
is there ever a chance that "wordsFound" variable will be greater than 10 (i.e. the value of LIMIT)?
1 2 3 4
if ( wordsFound == LIMIT ) // if more than LIMIT words are found, exit function
{
break;
}
Since it's a loop that iterates 1 value at a time, it will never actually go past 10. I've tested it out, if theres 11, 12, or 100 words, it only records first 10.
I made them local variables now, and i pass them by reference to save memory.