CODE

Nov 26, 2012 at 2:28pm
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?
Nov 26, 2012 at 2:32pm
cin and cout and if
Last edited on Nov 26, 2012 at 2:32pm
Nov 26, 2012 at 2:35pm
thanks but i need the code for it. i just cant figure it out
Nov 26, 2012 at 2:39pm
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 Nov 26, 2012 at 2:40pm
Nov 26, 2012 at 2:47pm
thanks man you are a life saver
Nov 26, 2012 at 8:08pm
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.
Nov 26, 2012 at 9:10pm
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.
Nov 26, 2012 at 10:54pm
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::?
Nov 27, 2012 at 12:16am
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.
Nov 27, 2012 at 3:07pm
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!
Nov 27, 2012 at 4:16pm
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.
Nov 27, 2012 at 7:41pm
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.