1

So, I was running my code and I would like to input a string. But It skips to the next input prompt. Here's the code : It starts with the "Enter Product Name" but instead, it skips right away to "Enter Product Type"

    if (path == 3) {
        std::ofstream ReferenceList;
        while (again == "y" || again == "Y") {
            ReferenceList.open("RefList.txt", std::ios::app);
            std::string name, type, iprice;
            std::cout << "Enter Product Name";
            std::getline(std::cin, name);
            std::cout << "Enter Product Type: ";
            std::getline(std::cin, type);
            std::cout << "Enter Y to Add Product Again: ";
            std::cin >> again;
            ReferenceList.close();
        }
    }

It skips right away to "Enter Product Type".

Here's the prompt display:

Enter Product NameEnter Product Type:

I would like to Enter the Product Name before Entering the Product Type.

thermoplay
  • 75
  • 8
  • Please post a [mcve]. That's too much code, IMO. – R Sahu Mar 17 '20 at 05:00
  • Also, there are quite a few posts on SO on the topic. You can start from https://stackoverflow.com/questions/29082621/getline-skips-line-when-updating-stuct-in-loop. – R Sahu Mar 17 '20 at 05:02
  • I did that, so in case the problem was in the topmost part of the code, then it can be evaluated here, I instead post my whole code than giving an incomplete one. I also specified where the problem occurred, focusing on the specific part of the code. – thermoplay Mar 17 '20 at 05:03
  • @thermoplay The value of minimal reproducible example is people could use little time to reproduce your problem and answer you in a relative short time. A bunch of code will consume too much time and raising the probability of your question not being answered. You did well on second block. I'll suggest you remove first code block and clean second code to minimum reproducible. – Louis Go Mar 17 '20 at 05:10
  • If you make you MRE more minimal (while still reproducable) you will more easily and faster find help. It also is very likely that while removing irrelevant parts of the demonstration code you will find the problem yourself or realise the relevance of the link provided by R Sahu. – Yunnosch Mar 17 '20 at 05:12

1 Answers1

0

try to use

std::cin.ignore();

to flush the input buffer before loop.

maybe at first line in while loop like this:

    if (path == 3) {
        std::ofstream ReferenceList;
        while (again == "y" || again == "Y") {
            std::cin.ignore();//flush the input buffer
            ReferenceList.open("RefList.txt", std::ios::app);
            std::string name, type, iprice;
            std::cout << "Enter Product Name";
            std::getline(std::cin, name);
            std::cout << "Enter Product Type: ";
            std::getline(std::cin, type);
            std::cout << "Enter Y to Add Product Again: ";
            std::cin >> again;
            ReferenceList.close();
        }
    }
tletle
  • 406
  • 5
  • 5
  • this was my first solution before I posted this question. I can say that this works, but is there no other way besides using the ignore function? – thermoplay Mar 17 '20 at 05:35