Hello adam201,
Some thought about your code as i went through it.
For now and more so in the future do your self a favor when you refer to something in "
Bjarne's" book make a reference to the page number and anything else that helps to know what you are working from. Many people have his book, but do not want to spend time trying to find what you are working.
You left out the header file "string".
You could define your vector as:
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
|
#include <iostream>
#include <string>
#include <vector>
//using namespace std; // <--- Best not to use.
// <--- These worked fine where they were, but are better placed at the beginning of the functions.
int sentence();
int noun();
const std::vector<std::string> ALLOWEDWORDS
{
"C++", "birds", "fish", // <--- Nouns.
"rules", "fly", "swim", // <--- Verbs.
"or", "and", "but", // <--- Conjunctions.
"." // <--- Punction.
};
// <--- A 2D vector where each row is a different type. Just a thought and some of the code would have to be adjusted.
//const std::vector<std::vector<std::string>> ALLOWEDWORDS
//{
// {"C++", "birds", "fish"}, // <--- Nouns.
// {"rules", "fly", "swim"}, // <--- Verbs.
// {"or", "and", "but"}, // <--- Conjunctions.
// {"."} // <--- Punction.
//};
// <--- Try to avoid using global variables like these.
std::vector<std::string> words;
int pos = 0;
|
The 2D vector is just an idea.
By defining the vector this way you can eliminate the need for the function "makeAllowedWords()" to populate the vector.
In you for loops at lines 52 and 56 you have
for(int i = 0; i < words.size(); i++)
. This is likely to generate a warning because the "i" and "words.size()" are two different types. If you look up the ".size()" for something, i.e., a string you will find that the "size" function in most of the classes return a "size_t" or on occasion a "size_type" both are a type def for an "unsigned int". So comparing an "int" to an "unsigned int" is a slight problem, but not enough to stop the compile or keep the program from running.
The last thing I find confusing is:
1 2
|
std::cout << "enter a sentence" << std::endl;
std::cout << "sentences must end with a space and . press shift * and enter key to enter the sentence" << std::endl;
|
Mostly line 2. I am thinking that the period should be enough to end the while loop and not the "*". Just my opinion.
Hope that helps,
Andy