简体   繁体   中英

Writing a program to print a “Hello, world!” program

I just started reading Accelerated C++ and I'm trying to work through the exercises when I came across this one:

0-4. Write a program that, when run, writes the Hello, world! program as its output.

And so I came up with this code:

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{
    cout << helloWorld << endl;

    cin.get();
    return 0;
}

void helloWorld(void)
{
    cout << "Hello, world!" << endl;
}

I keep getting the error 'helloWorld' : undeclared identifier . What I figured I was supposed to do is make a function for helloWorld then call that function for the output, but apparently that's not what I needed. I also tried putting helloWorld() in main, but that didn't help either. Any help is greatly appreciated.

The way I read the textbook exercise is that it wants you to write a program which prints out another C++ program to the screen. For now, you need to do this with a lot of cout statements and literal strings surrounded by "" s. For example, you can start with

cout << "#include <iostream>" << std::endl;

You're not actually calling your helloWorld function anywhere. How about:

int main()
{
    helloWorld(); // Call function

    cin.get();
    return 0;
}

Note: You'll also need to declare your function prototype at the top if you want to use it before it's defined.

void helloWorld(void);

Here's a working sample .

To call a function, you need to:

  • Provide a declaration prior to its use
  • Follow its name with a pair of parantheses, even if it doesn't have any arguments.
  • Provide a return value in order to use it in an expression.

For example:

std::string helloWorld();

int main()
{
   cout << helloWorld() << endl;
   ...
}

std::string helloWorld()
{
    return "Hello, world!";
}
hellwoWorld();

而不是cout << helloWorld << endl;

In your main function, helloWorld is not a declared variable.

You want hellowWorld to be a string whose contents are the hello world program.

depending on the compiler you are using, you might need to put helloWorld function before your main like this.

void helloWorld(void)
{
    .....
}
int main()
{
    .....
}

I use visual studios and I am forced to do this ....

You dont really need the helloworld function defined at the bottom. Something like this should do it.

#include "stdafx.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //cout will output to the screen whatever is to the right of the insertion operator, 
    //thats the << to it's right. 
    //After "hello world" the insertion operator appears again to insert a new line (endl)
    cout << "hello world" << endl;

    //cin.get()  waits for the user to press a key before 
    //allowing the program to end
    cin.get();
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM