-4

I am trying to write the following code so that it might return Hello, World!.

using std::string;
using namespace std;
std::string hello()
{
    return "Hello, World!";
}
int main()
{
    hello();
    return 0;
}

having the code compiler run with 0 errors, nor Waring, and run, then a bank result as shown in the attached pic. Thus can anybody figure out what's missing such that it returns the desired result!

running the code

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • 6
    Are you trying to print the string? Try `std::cout << "Hello, World!;"` – Blaze Oct 16 '19 at 08:26
  • 3
    About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Oct 16 '19 at 08:27
  • To go with the beginners book recommendation, that should teach you that system header like `iostream` and `string` should be included using angle-brackets `<>`, as in `#include `. – Some programmer dude Oct 16 '19 at 08:29
  • @Blaze in their case `cout << hello()` – Federico klez Culloca Oct 16 '19 at 08:29
  • actually i want to use similar function in the future so that i would not be having to write the message every time – prof_shadi Oct 16 '19 at 08:31
  • 1
    ??? When should which function would print what "every time". I believe I am not the only one who can't understand what you want to tell us! – Klaus Oct 16 '19 at 08:33
  • You should format your code properly so it is readable. Look how the samples in your beginner's C text book are formatted. Also don't post pictures of text but post text as text. – Jabberwocky Oct 16 '19 at 08:38
  • You might be confusing the two common senses of "output": it can mean both "something that a program prints" and "something that a function returns to its caller". – molbdnilo Oct 16 '19 at 08:54

1 Answers1

4

Your function hello() returns the string, "Hello, world!" but, after you've called that function from main, you don't do anything with the returned value.

You need to output something! Try this minor modification:

int main ()
{
    std::cout << hello() << std::endl; // So you can avoid "using namespace std!"
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83