0

Not sure what is going on with my code. The issue is with my getline function. Using the code snippet:

string searchItem;
cout << "Enter the name of the item: ";
getline(cin, searchItem);
cout << searchItem << endl;

I have put this snippet in a program by itself and it works fine but when I use it in my code it skips right over it as if there is no cin at all. The program does not pause to let the user enter anything. Even taking the below portion of code and putting it in a different program it still does the same thing. I did not want to put the full code since it is a homework assignment.

#include <iostream> // used for standard I/O
#include <istream>
#include <fstream>  // used for file I/O
#include <string>   // used for string var
#include <vector>   // used for vectors
#include <iomanip>  // used for setprecision()


using namespace std;

void menu();

int main()
{
    menu();
}

void menu()
{
int mainMenuChoice;

cout << "Choose among the following options." << endl;
cout << "1: To see if an item is in the store." << endl;
cout << "2: To buy an item." << endl;
cout << "3. To check the price of an item." << endl;
cout << "4: To print the inventory." << endl;
cout << "9: To end the program." << endl;
cin >> mainMenuChoice;

cout << endl;
cout << endl;

// To see if an item is in the store.
if (mainMenuChoice == 1)
{
    int i = -1; // inventory location 
    string searchItem;
    cout << "Enter the name of the item: ";
    getline(cin, searchItem);
    cout << searchItem << endl;

    menu();
}
}

0 Answers0