Since I said I might look, I started checking out how you are reading "words" and you would stand to gain by learning about streams. stringstreams, that is.
BTW, did you know that this:
1 2 3 4 5 6 7 8
|
for(unsigned int i = 0; i < s.length(); ++i){
if(s.substr(i,1) == " ")
break;
else
spacecheck += 1;
if (spacecheck == s.length() )
word.push_back(s);
}
|
Is actually this:
1 2 3 4 5 6 7 8 9
|
for(unsigned int i = 0; i < s.length(); ++i){
if(s.substr(i,1) == " ")
break;
else {
spacecheck += 1;
}
if (spacecheck == s.length() )
word.push_back(s);
}
|
An "un-braced" condition like if, else if, else, and [all of the loops]. will only match the following 1 line.
The way the code was tabbed I didn't think so, but maybe.
This is a great place for stringstream!
You will have to add <sstream> to your includes.
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
|
int main(){
//while(true){
string s;
stringstream ss;
vector<string> word;
cout << "Enter a sentence: ";
getline(cin, s);
size_t found;
size_t found1;
ss.str(s); // fills the stringstream with contents of "s"
while( !ss.eof() ) // stop if end of stream
{
ss >> s; // read 1 word from ss in to s, automatically removing spaces
if (ss.good()) // if read last string succeeded
word.push_back(s); // add it to our vector of words
else
{
ss.clear(); // clear stream state flags - probably not needed
// if ss is not again used as an input-stream, but still it is
// good practice because if you do this on cin - which I think you can - further stream reads will fail.
break; // stop reading on bad input (even though I don't
// think it will fail to read an integer into a string,
// but I don't recall well at this time without trying.)
// the final read should be on an empty stream so you
// would want to make the stream "good" again
}
}
|
With this change, I think you can get rid of this, which appears to be used for the case where you only had 1 word and your "look for a space-character" search failed.
1 2 3 4 5 6 7 8 9 10
|
if (word.size() == 0) {
found1 = s.find_first_of(" ");
while(found1 != string::npos){
word.push_back(s.substr(found,found1-found) );
found = found1 + 1;
found1 = s.find_first_of(" ", found1 + 1);
}
found1 = string::npos;
word.push_back(s.substr(found, found1) );
}
|
Your "initVect()" function can be replaced with vector's member function".clear()"
1 2 3
|
word = initVect(word);
// becomes:
word.clear();
|
I didn't get to look at everything, but you should be able to do about this if you include <algorithm>:
1 2 3 4 5 6 7 8 9 10
|
cout << "What word would you like to search for?: ";
cin >> sWord;
vector<string>::iterator loc;
loc = find(word.begin(), word.end(), sWord);
if (loc != word.end()) // I _think_ this is the correct test
cout << "I found it!" << endl;
else
cout << "I didnt find that" << endl;
|
Otherwise, if you dont like the <algorithm> for some reason you can iterate over it yourself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
bool weFoundIt = false;
vector<string>::iterator b,e; // Begin and End
b = word.begin();
e = word.end();
while(b!=e)
{
if ( *b == sWord ) // a match? ( the "*b" is the value at that iterator)
{
weFoundIt = true;
break; // we can stop searching
}
b++; // get the next word (if this one isnt it)
}
if (weFoundIt)
{
cout << "Yep! Found " << sWord << endl;
}
|
See if you can figure out min/max with these iterators. :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
string tempString;
vector<string>::iterator b,e;
b = word.begin();
e = word.end();
while( b != e ) // from begining to end of the vector of words
{
tempString = *b; // tempString now holds this word
// de-referencing an iterator is kind of strange but
// it is really no different than a pointer here (kinda.)
// tempString is now a standard "string" object
// do your testing for length here and process result.
+++b; // next word, please
}
|
Learning is fun. Fun!
But... "re-inventing the wheel" should be avoided, unless you really want to know how the "wheel" words (and when learning, I think you should look at it at lest some.) If you need to dig a hole in your yard, you dont invent a shovel do you? It is great for leaning, but after programming for many many years, I can tell you that it is far better to simply use the tools that have already been created for you.
There is a ton of code that is already written to do most trivial problems (and if you get to the non-trivial stuff, you will probably find that "boost" already has it - and a lot of people will recommend that you try it out.)
If you really want your mind-blown, then look up a "BST". I think that is the solution to the "first problem" that we used in Comp Sci.