Hiya

I don't know what I'm doing wrong that getline() does not work for me. My program is not finished but this is what I got so far:

#include <iostream>
#include <vector>
#include <string>
using namespace std;


struct Term {

    string word;
    string definition;

};

struct Dictionary {

    vector<Term> term;

};

void helpFunction() {

        cout << "You have the following three options to choose from. \n" << 
        "Note: Case sensitive, type them as exactly as they appear: \n" << endl;
        cout << "Type 'Search' at anytime to search for a word "<< endl;
        cout << "Type 'Help' at anytime to see the menu options again"<< endl;
        cout << "Type 'Exit' at anytime to exit the program"<< endl;


}//end help

void infoFunction() {

        cout << "Welcome to the best Dictionary program ever...EVER! \n" << endl;
        cout << "Use this program to find a word and its definition. If the word is not in the library, you will be asked to update the library. \n" << endl;
        cout << "You have the following three options to choose from. \n" << 
        "Note: Case sensitive, type them as exactly as they appear: \n" << endl;
        cout << "Type 'Search' at anytime to search for a word"<< endl;
        cout << "Type 'Help' at anytime to see the menu options again"<< endl;
        cout << "Type 'Exit' at anytime to exit the program"<< endl;


} // info function

void programLoop() {

    //variables to use for program
    string userInputString = "";
    string searchString = "Search";
    string helpString = "Help";
    string exitString = "Exit";

    infoFunction();


     do {

         cin >> userInputString; //get string input


         if ( userInputString.compare(searchString) == 0 ) { //search for word


        string userDefinition;
                string userWord;
        Dictionary myDictionary;

        bool matchFound = false;

        if ( myDictionary.term.size() == 0 ) {//assume no words in dictionary


        cout << "Input the first word into the dictionary: ";
        getline (cin,userWord);
        cout << userWord << " .\n";


         } //end find word section



        if ( userInputString.compare(helpString) == 0 ) //help menu
        {

            helpFunction();

        }

        if ( userInputString.compare(exitString) == 0 ) //exit

        {
            exit(0);
        }

     } while ( userInputString.compare(searchString) == 0 || userInputString.compare(helpString) == 0 || userInputString.compare(exitString) == 0 );



} // program loop




void main()

{
    programLoop();
    system("pause");


} //end main

My cout of userWord is just to see if getline is working...which is not for some reason :( The funny thing is I know getline should work! I have already wrote a small little program (thats rather useless) to test it out (see below)I'm baffled by this. There must be something I'm not seeing or are not aware off Any help appreciated. :(

This works:

#include <iostream>
#include <string>
using namespace std;


void main() {

  string str;
  cout << "Please enter a word: ";
  getline (cin,str);
  cout << str << ".\n";

  string def;
  cout << "Please enter the definition: ";
  getline (cin,def);
  cout << str << " : " << def << ".\n";


    system("pause");

} //end main

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Mixing cin and getline like you do can cause problems. Since you press enter after entering in userInputString and cin does not take up that resulting '\n', it stays in the stream and gets taken up by the getline (and interpreted as the end of the input, so control appears to "skip" over it).

See the sticky thread in this forum about flushing the input stream for more details. However, in this case, with one stray '\n' it is probably sufficient to put a cin.ignore(); before your getline statement.

I had a feeling it was something like that.

Thanks a lot :) I had already tried adding the ignore but after the getline, did not occur to me to put it before.

Anyway thanks again

This question has already been answered. Start a new discussion instead.