0

I am confused even as to how I could ask this question. Essentially, when I attempt to put a space in between two words in a quotation in an if statement and the user ends up typing that quotation with a space, it doesn't end up working. It's hard to explain, you may have not even got what I just said. Just take a look at my code and it will all make sense.

cout << "Type which section you would like to go to: " << flush;
    string section;
    cin >> section;

    if (section == "Hot Coffees" || section == "hot coffees" ) {
        cout << "Here is the list of all the hot coffees we sell." << endl;

Now if the user attempts to type 'Hot Coffees' or 'hot coffees, for some reason it is not printing out 'Here is the list of all the hot coffees we sell.' It only works if I remove the space or replace 'Hot Coffees' with 'Coffee'. Did I do something wrong? Does C++ not allow for spaces in these quotations? Thank you!

ssingh
  • 21
  • 5
  • If statement is fine, your reading method is not. – Yksisarvinen Jul 23 '20 at 12:39
  • I was just taught this way. What should I do instead? Thanks for answering though! :) – ssingh Jul 23 '20 at 12:41
  • See the duplicate question for answer (and please scroll down, accepted answer isn't quite good, most upvoted answer is). `std::cin >>` will stop reading on first whitespace, so you need e.g. `std::getline` instead. – Yksisarvinen Jul 23 '20 at 12:45
  • Yeah that's what I was looking at. Thank you! – ssingh Jul 23 '20 at 12:45
  • Just one suggestion, comparing strings like that is a bad idea, at least transform the string you are comparing into all lower characters or all upper characters – vikAy Jul 23 '20 at 12:49
  • When you use standard input (std::cin) the program will stop reading at the whitespace. But when you use getline input the program will stop reading at the newline character. – User123 Jul 23 '20 at 12:52

0 Answers0