Could anyone tell me why, in the following code, getline doesn't allow the user to enter any input? Thanks in advance.

#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char *argv[])

{   
    char answer = 'n';
    int selection;
    vector <string> strings;
  
  do {  
    cout << endl;
    cout << "------------------MAIN MENU-------------------" << endl << endl;
    cout << "1. Add a string to the end of a vector." << endl;
    cout << "2. Display AND remove the first element of the array" << endl;
    cout << "3. Display and remove the last element of the array" << endl;
    cout << "4. Display the number of elements in the vector" << endl;
    cout << "5. Print the contents of the vector. 'Empty' if empty" << endl;
    cout << "6. Print the contents of the vector backwards." << endl;
    
    cin >> selection;
    
    if (selection == 1) {
       char answer = 'n';
       //HERE IS THE PROBLEM
       do {
          string entry;
          cout << "Enter a string to add to the end: ";
          getline(cin, entry);
          cout << "You entered " << entry;
          strings.push_back(entry);
          cout << "Would you like to enter another string? [Y/N]: ";
          cin >> answer;
          } while (answer == 'y' || answer == 'Y');
    }
    
    if (selection == 2) {
       vector<string>::iterator position;
       position = (strings.begin());
       int i = 0;
       cout << "Deleting " << strings[i] << endl;
       strings.erase(position);
    }
    
    if (selection == 3) {
       vector<string>::iterator position;
       position = (strings.end()-1);
       cout << "Deleting " << strings[(strings.size()-1)] << endl;
       strings.erase(position);
    }
    
    if (selection == 4) {
       cout << "There are " << strings.size() << " elements in the vector." << endl;
    }
    
    if (selection == 5) {
       if (strings.size() == 0 ) {
          cout << "Vector is empty." << endl;
       }
       else {  
            for (unsigned int i = 0; i < strings.size(); i++) 
                cout << strings[i] << endl;
            }
    }
       
    if (selection == 6) {
       for (int i = strings.size() -1; i>=0; --i) {
           cout << strings[i] << endl;
       }
    }
    
    
    cout << "Would you like to make another MENU selection? [Y|N]";
    cin >> answer;

  }while (answer == 'y' || answer == 'Y');
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

Recommended Answers

All 3 Replies

Pop a cin.ignore() between lines 32 and 33 (your input was getting corrupted, that throws out anything left over).

Also, don't redeclare answer on line 28.

Pop a cin.ignore() between lines 32 and 33 (your input was getting corrupted, that throws out anything left over).

Also, don't redeclare answer on line 28.

Thanks!!! That fixed it. Out of curiosity, why was the input getting corrupted?

Could be the '\n' from when you hit enter after the numeric selection, it might stay stuck in the stream.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.