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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With