So upon researching online for code I came across one that works great, but I don't understand completely. I just want to understand what is happening throughout the code, why this person did some of the things they did. Does that make sense? I'm seeing a lot of things I haven't learned yet.
The program converts english phrases to pig latin.
#include <sstream> //Why use this and the following?
string pigIt(const string&); //Why const?
int main(int argc, char *argv) //Why does this need a pointer? (that's a pointer, right?)
stringstream ss; //So I assume this has connection to the <sstream>, but what does this do?
if (line.size() > 0) //line size gets the number of characters in a string, right?
ss.clear(); ss.str(line); //Why do we clear the string? Is that what ss.clear does?
cout << ": ";
while (ss.good())//Variable? (ss.good)
ss >> word; //What does the command ss do? Is it another input command?
string pigIt(const string &w) //again, how do you know when to use a constant?
pig.erase(0,p); //is p a single character place holder for the first(0) character?
staticconst string vowels("aeiou"); //What is static constant?
return (vowels.find(tolower(c)) != string::npos); //I know npos means no positino, but how does this line work? Is it an if statement?
Thanks for identifying your questions.
All line references are to your current post, except where noted.
line 1: because you use stringstrem at line 8.
line 4,18: As ats15 said, tells the compiler you don't modify w within pigIt. The compiler will flag an error if you attempt to modify a const argument. This has two benefits. 1) It protects against accidently modifying something you didn't intend to modify. 2) It lets the compiler generate more efficient code since the compiler doesn't have to worry about saving any changes to the variable.
line 6: argv is actually an array of pointers (note the ). argv is a pointer to a character array containing the program file name. argv is a pointer to a character array containing the first argument. A null pointer follows the last command line argument. argv should be considered const although most compilers (for historical reasons) won't flag an error if you don't declare argv as const.
line 8: ats15 gave you a reference to stringstring. Yes, you need the <sstream> header if you use stringstream. stringstream is convenient if you you have a string containing data fields and want to parse it a field at a time using the >> operator. stringstream can also be use to build output into a string using the << operator.
line 10: correct.
line 12: Yes, clear clears the string. clear is needed here because ss was declared outside the loop (orig line 15). If you don't clear it, ss would just accumulate everything you output to it.
line 14: ss.good() is a function call (note the () ). It tests the good bit for the stream. stream inherits from iostream, so it has all the properties of an iostream. See the documentation for iostream for what can set/clear the the good/bad bit.
line 16: Inputs a string (word) from the current line. This is just like an input operation from cin, except that it uses a string (line) as the source of bytes to read instead of a file. Note that ss was constructed from line at line 19 in your original post.
line 20: No. p is an unsigned int. See line 36 in orig code. It functions as an index.
line 22: static tell the compiler to allocate vowels only once. i.e. it is not created each time isVowel() is entered. You can think of it something like a global in that it always exists, but is only visible to isVowel.
line 23: This is a boolean expression. It evaluates to true or false. If find returned something other than npos, then the result of the expression (and the function) is true.
edit: removed line 12 comment per Dante12129's post.