#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string str;
int i = 0;
int length = str.length();
cout << "Please enter a phrase (one or more words, all characters in lower case): ";
getline (cin, str);
if (str == "sir")
cout << "matey." <<endl;
if (str == "madam")
cout << "proud beauty." << endl;
if (str == "officer.")
cout << "foul blaggart." << endl;
If I put in the phrase "sir" "madam" "officer" it doesn't recognize it. But it does recognize "sir" = "matey." So I'm basically asking on how to make the console recognize all of these in a sentence. combine these phrases using a while loop.
I think you want str.find() for each word in the line.
this approach is very challenging to work with for a large word list.
consider putting all the words and their translations into some sort of container so you can just swab one word for another easily using str.replace ??
You have the basics down, using std::getline to retrieve the entire line in a std::string.
To parse out the individual words create a std::stringstream from your input string and then loop reading the individual words in the stream as you would read from std::cin. Check to see if the failbit or eof isn't set and continue with another loop read.
With string streams you should note that you're losing white-spaces. So if there were 30 white-spaces, that wouldn't be observed in the output, only one white-space would (because we put one white-space between words).
Another thing to note is that string stream might fail if you have punctuation. So to counter this I have made it so that if there is only one successive character after the phrase then the program should still translate the phrase and preserve the character.
However that character need not be a punctuation, it could be any character. So those optimizations parts are up to you. You have to verify that it's actually a punctuation by adding another comparison.
Also note that the comparison will not consider cases. So if you write "Madam" then the program won't recognize it. If you want to recognize cases then you need to convert both the phrase and the comparison phrase to either uppercase or lowercase. You can use transform.
If you are going to use the string stream approach that furry guy mentioned and for which I demonstrsted, note that because you are parsing word by word you won't be considering whitespaces and hence will lose all whitespaces from the original input.
Parsing word by word is the only reason you would be using string stream in this context. Otherwise you would just use strings.
I don't think the program is supposed to be that complex, and I'm only supposed to use <string>. I also don't recognize most of the functions/strings you used there.
If you cannot use a structure then just use two c-style arrays. And enclose the entire thing inside a for loop which iterates once per word in the array to find the word in the string.