CODE

What code do i need to add into a console application so when the user types in Help (in upper and lower case) it will tell them what to do?
cin and cout and if
Last edited on
thanks but i need the code for it. i just cant figure it out
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
    std::string input;
    std::getline(std::cin, input);
    if(input == "Help")
    {
        std::cout << "Please hang up and try again." << std::endl;
    }
}
If you want it yo work for any case variation, the easiest way is to go through can convert all capital letters to lowercase letters:
1
2
3
4
5
6
7
for(std::string::iterator it = input.begin(); it != input.end(); ++it)
{
    if(*it >= 'A' && *it <= 'Z')
    {
        *it = *it - ('A'-'a');
    }
}
You should try to rewrite it in code that you understand and then incorporate it into the example program.
Last edited on
thanks man you are a life saver
I've been learning C++ for a week or two now, and in the book I've been reading and examples I've been seeing, couldnt you remove all of the std:: at the beginning of each peice of code, and at the beginning but using namespace std;? Sorry if theres an obvious reason for doing it your way, I'm just curious.
You definitely could, but it is strongly discouraged for good reason. It takes everything in std and brings it into the global namespace. This generally causes ambiguities (std::count mixing up with your loop counter named count) and a whole host of other problems that can be avoided by not using it. For test programs it's obviously fine, but I and many others try to avoid doing it for the purpose of not getting into the habit of it - it causes real problems in real code, and if you'll find yourself using more duct-tape than C++, or worse, having to go through and remove "using namespace std;" and add "std::" everywhere at a late stage in your project when you have thousands of lines of code.

A 1 degree angle isn't very much and seems harmless, but try going to the moon and being one degree off in your trajectory.
Would you suggest following the using namespace std; method while learning since that is how examples are provided to me, until the time comes that I'm taught to use each individual std::?
No. You should start out writing std:: before them. It will also help to show you which things are from C++ and which are from C (the C stuff isn't in std). It is better to start a habit early than to try to change a habit later.
Guess I'll have to use another guide, or skip to a part in the book where you learn to know when to use std:: I do not want to create bad habits for myself. Thank you!
The idea is that the examples are easier to read and understand, the side-effect is that people think it is the correct way to write code. You can keep using the same guide, just remember that they don't write std:: because they want you to not get too distracted.
If you really don't want to use std::, then you can replace using namespace std; with using std::cout; using std::endl; using std::cin; and explicitly stating any functions/objects that you are bringing in from the std namespace. That'll avoid any accidental conflicts.
Topic archived. No new replies allowed.