1

This is my code.

#include <iostream>
#include <string>
using namespace std;

int main() {

    cout << "Welcome to Starbucks! What would you like to order?" << endl;
    cout
            << "Before you select your order, would you like to look at your wallet to see how much money you have on you right now? "
            << flush;

    string input;
    cin >> input;

    if (input == "Yes" || input == "yes") {
        cout << " " << endl;
        cout << "OK. You have $18.90 in your wallet. Now showing menu. "
                << flush;
    }
    else {
        cout << " " << endl;
        cout << "OK. Now showing menu. " << endl;
    }

    cout << "\n\n\tDrinks:\n\nHot Coffees\nHot Teas\nHot Drinks\nFrappuccino Blended Beverages\nCold Coffees\n\n\tFood:\n\nHot Breakfast"
            "\nBakery\nLunch\nSnacks & Sweets\nYogurt & Custard\n\n\tMerchandise:\n\nDrinkware" << endl;

    string option;

    cout << "Select an option from one of these sections. Type your selection here: " << flush;
    getline(cin, option);

    if (option == "Hot Coffees" || option == "hot coffees" || option == "Hot coffees" || option == "hot Coffees") {

        cout << "Welcome to the Hot Coffees section. Here is a list of all the hot coffees we sell:\n\nAmericanos:\n\nCaffè Americano\nStarkbucks Blonde Caffè Americano\n" << endl;
    }



    return 0;
}

At the very end, when I decide to use a getline function (I want to get the input of the user to use in if statements), I am unable to type into my console for an unknown reason, therefore making me unable to get the user's input. I have already tried cin.ignore(); , and although this let me type into the console, after I typed the words "Hot Coffees", it didn't print out "Welcome to the Hot Coffees section.", even though I programmed my if statement to print this out if(option == "Hot Coffees"). If you can figure this out, thank you! If you can't, I am certainly glad you at least tried, and I will be just as thankful.

ssingh
  • 21
  • 5
  • Does this answer your question? [Using getline(cin, s) after cin](https://stackoverflow.com/questions/5739937/using-getlinecin-s-after-cin) – dxiv Aug 05 '20 at 03:21
  • How should I use it for my code, however? – ssingh Aug 05 '20 at 13:10
  • Or were should I use It? I'm not very familiar with get line(cin,s) after cin – ssingh Aug 05 '20 at 13:15
  • 1
    You run into the exact same problem described in the linked question (and its duplicate), so any of the answers there will apply - use `get()` / `get(c)` or `skipws` or `ignore` or `getline` twice etc, – dxiv Aug 05 '20 at 15:53

2 Answers2

1

It's because C++ doesn't consider spaces, so try typing cin.ignore(); after getline function. And you made mistakes in the if section, type this....

if (option == "Hot Coffees" || "hot coffees" || "Hot coffees" || "hot Coffees") {

    cout << "Welcome to the Hot Coffees section. Here is a list of all the hot coffees we sell:\n\nAmericanos:\n\nCaffè Americano\nStarkbucks Blonde Caffè Americano\n" << endl;
}
alphaX64
  • 71
  • 1
  • 7
  • See, my main problem is that I want to use the input of the user, stored in "option", for multiple different if statements. For example, if (option == Hot Coffees), I want to go to the Hot Coffees section. if (option == Hot Teas), then I want to go to the Hot Teas section. I want to do that for every item in my list stated in the "cout" above. But it just refuses to work. – ssingh Aug 05 '20 at 13:34
  • Although cin.ignore(); did let me type "Hot Coffees" into the console, it did not print out "Welcome to the Hot Coffees section." afterwards, which is what I had programmed my if statement to do. Thanks for trying though! :) – ssingh Aug 05 '20 at 13:48
  • `string HCoffees; cout << "\nPlease choose one of these hot coffees. Type your selection here: " << flush; std::getline(std::cin.ignore(), HCoffees)` – ssingh Aug 06 '20 at 03:30
  • But nothing happens, it only leaves a blank space. Did I do something wrong? – ssingh Aug 06 '20 at 03:34
  • Dude! I got it! You made mistakes in the code! Check the edited part of answer above – alphaX64 Aug 06 '20 at 04:52
  • I'm a bit confused. What did you find out? Isn't that my if statement that I had previously? – ssingh Aug 06 '20 at 15:27
  • It looks kind of the same to me. – ssingh Aug 06 '20 at 15:28
  • OK ! So, just type cin.ignore after getline fn then replace the if statement with the above. I tried and it worked! – alphaX64 Aug 07 '20 at 03:25
  • You made mistakes in the conditions, that's why the code doesn't execute properly – alphaX64 Aug 07 '20 at 03:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219382/discussion-between-airax86-and-ssingh). – alphaX64 Aug 07 '20 at 03:26
  • I don't mean to annoy you, but isn't that what I did (typing cin.ignore after getline) in the code in the comment section? – ssingh Aug 17 '20 at 23:35
1

It's likely that you still have a \n in your input buffer, so when you reach getline() it's reading that as the delimiting character, and returning that as input for you. I think that std::flush might do something different than you think it does. This is a helpful read: https://www.cplusplus.com/reference/ostream/flush-free/

Here's a line of code that's useful when dealing with while user input on the console in C++, It gets every character in the input buffer until it finds a \n.

while (cin.get() != '\n');

Best of luck to you!

Liam Gallagher
  • 170
  • 1
  • 9