hello world

Hi! im creating something i like to call a complex hello world. im extremely new to coding let alone c++ and to start i made a hello world. i know how to do that with ease now so i decided to try and make something a little more complicated. I'd like my program to first ask "Would you like to see the hello world?" and then based off of the user inputing "yes" or "no" it will either respond with "hello world" or close the program. i thought that I could possibly use booleans for this but im stuck. I need to know how to create a code that reads what the user types, like "yes" and then outputs the hello world.

Like:

 
 if (the user"s answer) = yes cout << "Hello world!" << endl; 
Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
	std::cout << "Would you like to see the hello world (yes/no)? ";

	std::string ans;

	std::cin >> ans;
	if (ans == "yes")
		std::cout << "Hello world!\n";
}

Hello KarlisPerez,

I would suggest this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
    std::cout << "Would you like to see the hello world (yes/no)? ";

    char ans;

    std::cin >> ans;

    if (ans == 'y' || ans == 'Y')
    {
        std::cout << "Hello world!\n";
    }
}

By using a string the if statement would have to cover choices, (ans), of "yes", "YES", "Yes", "YeS", "yES" and anything I have not thought of. The more characters you ask a user to type the more chance it will be wrong.

Unless your program is being used by people that always follow directions and never make a mistake you will need to try and account for anything that might be entered.

It would be a good habit to learn to use {} even when an "if" statement, "for loop" or "while loop" only has 1 line. Later it makes it easier to add to the block and if you forget to add the {}s just because the line is indented it is not part of the if statement.

Another way of doing your code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.
#include <iostream>
#include <string>

int main()
{
    std::cout << "Would you like to see the hello world (yes/no)? ";

    char ans;

    std::cin >> ans;

    if (std::tolower(ans) == 'y')
    {
        std::cout << "Hello world!\n";
    }
}

In line 13 you change the case and only have to make 1 comparison.

Andy
@KarlisPerez,

Both @seeplus and @Handy Andy gave good answers.

There is a subtle point that may be missed in @HA's reponse. In @seeplus' response, the program reads a string from the keyboard, and compares it to "yes". In @HA's response, the program reads a single char from the keyboard and compares it to 'y' or 'Y'. So the user only has to type 'y' to continue. The user can also type "yup!" or "you know what, I'm not interested". Only the first character is read and evaluated.

I'm not suggesting how to write your code. I'm just trying to clarify something that may have been missed.
@doug4,

Nice input. Thank you. I will try to that point in the future.

Andy
Topic archived. No new replies allowed.