0

Sorry to bother with this. This is a basic function, but I am still new the whole realm of C++. I am trying to allow any form of the word "hello". I want the program to allow input any form of hello, no matter how the user inputs the word (i.e. capitalized, lowercase or mixed). Feel free to critique, I am just trying to learn. :)

int instructions() 
{

    string intro;

        do
        {
            cout << "Greet me with 'hello' to continue: " << intro;
            cin >> intro;

        } while (intro != "hello");

        cout << "Welcome! What shall I call you?  ";
        cin >> name;
        cout << "\nHello " << name << "! ";

    return 0;
}
user207421
  • 305,947
  • 44
  • 307
  • 483
Kisner
  • 79
  • 9
  • 1
    Check this out http://stackoverflow.com/questions/313970/how-to-convert-stdstring-to-lower-case You just need to convert input to lowercase and then compare with 'hello' – m_pro_m Jan 02 '15 at 01:21
  • 3
    @m_pro_m It is typically a better idea to use a case-insensitive comparison algorithm. The conversion to lowercase has its own set of issues. (However, there is no such algorithm in the C++ Standard Library that I'm aware of. boost has one, though.) – dyp Jan 02 '15 at 01:24
  • 1
    @Rinzler This has nothing to do with the language, but with *libraries*. For example, [this](http://stackoverflow.com/a/315463/420683) is a very simple solution in C++ using a boost library. – dyp Jan 02 '15 at 01:25
  • 2
    @Rinzler There's no use in engaging in discussions in SO comment sections; eventually a moderator will delete them anyway. I agree that `std::string` is a confusing tool, but I'd argue it should do even less: simply store and manage zero-terminated sequences of characters. That's basically the only thing it's good for (and a reasonable single responsibility). -- IMHO there's nothing *logic* about expecting algorithms in a class. It's one convention, and great parts of the C++ Standard Library follow a different one that separates algorithms from data structures. – dyp Jan 02 '15 at 01:36
  • @Lotois Your question is about string case, not do/while or functions. Please provide an accurate title. – user207421 Jan 02 '15 at 02:13

1 Answers1

1

You can use a few functions, but tolower is the first thing that came to mind. This solution requires you to use the C++ 11 standard because of the lambda. Let me know if you can't, and I'll provide an alternative.

#include <cctype>
#include <algorithm>
#include <iostream>

int instructions() 
{
    string intro; 

    do {

        cout << "Greet me with 'hello' to continue: ";
        cin >> intro;

        for_each(intro.begin(), intro.end(), [](char &a) {
            a = tolower((unsigned char)a);
        });

    } while (intro != "hello");

    return 0;
}
Ryan
  • 14,392
  • 8
  • 62
  • 102