-6

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;
tadman
  • 208,517
  • 23
  • 234
  • 262
Seth. C
  • 59
  • 2
  • 5
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. – Ron Jan 17 '18 at 01:48
  • Sounds like you are on your way. Try to write the whole program down in pseudo-code (Code you understand but isn't valid c++ code). Then look up in documentations and tutorials how to do each step. As a first hint, the opposite of "cout" is "cin". – pastaleg Jan 17 '18 at 01:49
  • Find a tutorial online that involves “how to read a users input”, then keep building on that logic. Good luck, but I’m guessing your “question” will be closed soon due to lack of effort prior to asking a question. – Brien Foss Jan 17 '18 at 01:50
  • Possible duplicate of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) –  Jan 17 '18 at 04:06

2 Answers2

0

You will need to use something like std::cin to take users input. Use something like a std::string container to hold the alphabetic answer i.e. yes or y. Then check your input against your control and conditionally print Hello World.

#include <iostream>
#include <string>

int main()
{
    std::string ans;
    std::cout << "Would you like to see the \"Hello World\"?\n-> ";
    std::cin >> ans;
    if (ans == "yes" || ans == "y")
    {
        std::cout << "\nHello World!" << std::endl;
    }
    return 0;
}
Justin Randall
  • 2,243
  • 2
  • 16
  • 23
0

We can do this by using the iostream that is provided by c++. There are 2 ways we can do this, using scanf or cin for the input, but for simplicity reasons, we'll just use cin.

#include <iostream>

using namespace std;

int main() {
    cout<<"Would you like to see the hello world?\n";
    string answer;
    cin>>answer;

    if(answer=="Yes") cout<<"Hello, world!";
}

Explanation: We prompt the user by using cout, then using string, we check if the input equals Yes, if it does, then we output Hello, world! Otherwise, the code will terminate. The \n you see is for making a new line, just for cleaner format.