Search code examples
c++inputiostreamcinformatted-input

Unformatted/Formatted input


After working once, the loop does not take input for the string menuitem. But it works for menuprice.

struct menuItemType
{
    char menuitem[30];
    float menuPrice;
};

void getdata( menuItemType menulist[], int items)
{
    for (int i = 0; i < items; i++)
    {
        cout<<"Please enter the menu item:"<<endl;
        cin.getline(menulist[i].menuitem,20,'\n');
        cout<<"Please enter the price for the menu item:"<<endl;
        cin>>menulist[i].menuPrice;
        cin.ignore;
    }
}

Solution

  • You aren't actually calling ignore because you're missing the argument list parentheses:

    cin.ignore();
    

    This will only work if the \n immediately follows the input. If you want to be a little safer, you can do:

    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    

    which will discard everything up to and including the next \n character.