0

(Sorry, I know this question has been asked [and answered] before, but none of the solutions worked for me, because something about the way my code is set up is wonky and I have no idea which part that is).

Okay, I have a function, get_cookie_type, which lets the user choose from 3 types of cookie--Chocolate chip, sugar, and peanut butter. After they type an input, I make sure that whatever they put in is one of those 3 choices, and throw an error message if not. The problem is that for the "Chocolate chip" and "Peanut butter" choices, I always get the "bad input" message, clearly because they have spaces, and I have no idea how to get around this. I've tried messing around with cin.getline, but it still gives me the bad input message.

WHY IS THIS

  string get_cookie_type()
    {
    std::string cookieType;
    cout << "What kind of cookie is the customer asking for? (Enter 'Chocolate chip', 'Sugar', or 'Peanut butter', exactly, without quotes).\n";
    std::getline(std::cin, cookieType);
    while (cookieType !="Chocolate chip" &&  cookieType != "Sugar" && cookieType != "Peanut butter")
    {
        cout << "\nYou put your data in wrong, try again.\n";
        cin >> cookieType;
    }
  return cookieType;
}
  • `cin.getline` is much different than `std::getline`. – chris Jun 03 '13 at 22:06
  • Ah, that seems like precisely the sort of thing I would have overlooked in searching around. Mind explaining how so, and what I'd need to change? – Evan Johnson Jun 03 '13 at 22:08
  • `std::cin.getline` is for C strings and should frankly never be used. `std::getline` adjusts to the length of the input and works with a nice `std::string`. – chris Jun 03 '13 at 22:33

2 Answers2

1

Use std::getline(std::cin, cookieType) in the while loop. operator>> will stop at the very first space while std::getline by default stops at the newline.

It looks like you have characters left in the input stream. Add the following line before the first call to std::getline (and include <limits> header):

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Jesse Good
  • 50,901
  • 14
  • 124
  • 166
1

You should place std::getline(std::cin, cookieType); inside while. try:

    std::getline(std::cin, cookieType);
    while (cookieType !="Chocolate chip" &&  cookieType != "Sugar" && cookieType != "Peanut butter")
    {
        cout << "\nYou put your data in wrong, try again.\n";
        std::getline(std::cin, cookieType);
    }

actually, do{}while would be more appropriate.

Valentin H
  • 7,240
  • 12
  • 61
  • 111
  • That works...kind of. Once I type in, say, "Chocolate chip", it works, but for some reason it tells me that I put the data in wrong before I even type anything in. Do you have any idea why that would be? I mean, I have a std::getline(std::cin, cookieType); before it even gets to the while loop, so there doesn't seem to be a reason that the error message would show before I even type anything in. – Evan Johnson Jun 03 '13 at 22:26
  • @EvanJohnson: Could it be, because you call get_cookie_type() having something in buffer already? Try outputting, what value cookieType has e.g. with cout << "\nYou put your data in wrong ("<< cookieType <<"), try again.\n"; – Valentin H Jun 03 '13 at 23:01