Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

(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;
}
share|improve this question
    
cin.getline is much different than std::getline. –  chris Jun 3 '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 3 '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 3 '13 at 22:33

2 Answers 2

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.

share|improve this answer
    
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 3 '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 Heinitz Jun 3 '13 at 23:01

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');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.