-1
char option;
cout <<"What langauge would you like to translate from - English 'E' or French 'F': ";
cin >> option;
cin.ignore();

ifstream in;
in.open("Q3.dic");
size_t pos;
string phrase;
if (option == 'E')
{
    cout <<"please enter a sentence in English: ";
    getline(cin, phrase);
    for(string english, french; 
    getline(in, english, '\t') && getline(in, french);
    )
    {
        pos = english.find(phrase);
        if(pos != string::npos)
        {
            cout << french <<endl;
            break;
        }
    //cout << english << '\t' << french <<endl;
    }
}
else if (option == 'F')
{
    cout <<"please enter a sentence in French: ";
    getline(cin, phrase);
    for(string english, french; 
    getline(in, english, '\t') && getline(in, french);
    )
    {
        pos = french.find(phrase);
        if(pos != string::npos)
        {
            cout << english <<endl;
            break;
        }
    //cout << english << '\t' << french <<endl;
    }
}
in.close();
cout <<endl;

system("pause");
return 0;

}

Right now my code only works if I type only one word. But if I type in a sentence it won't ouput anything.

So my question is if the user input: hello good morning, How would I search for hello and good morning in my text file and output it.

This is an example of the text file:

today aujourd'hui

good bon

good morning bonjour

afternoon après-midi

good evening bonsoir

much beaucoup

user3011154
  • 11
  • 1
  • 2
  • Looks like you could do some research: "stackoverflow read file word sentence". – Thomas Matthews Apr 01 '14 at 17:25
  • It looks like you've used [my code for reading in the dictionary](http://stackoverflow.com/a/22772641/78845) for parsing user input, which it won't do. If you're going to ask people to write your code for you, you're going to need to understand what it does! – johnsyweb Apr 01 '14 at 22:02
  • possible duplicate of [I want to translate from english to french and viceversa](http://stackoverflow.com/questions/22768038/i-want-to-translate-from-english-to-french-and-viceversa) – johnsyweb Apr 01 '14 at 22:03

1 Answers1

1

Looks like you need a three-step process:
1) Read in a line of text.
2) Extract the English word from the text string.
3) Extract the French words from the text string.

There is a good data structure you should look into: std::istringstream. This allows you to treat a string as an input stream.

Another method is to use the find methods of std::string.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154