OK. I've got a problem. Before I start, I'm using Xcode 2.0/Mac OS 10.4/PowerPC. I'm trying to learn C++, so I took out a book from the library and it's showing me good ol' HelloWorld. I ran into a problem. Here what the error message says. Here the code I was trying to compile. Can anyone help me?
OK. That fixed the problem. Thanks. But now I have another. The book says that the words "Hello World" should be on the screen, but it is blank.
Whatever book says is correct but you should be able to see that but it looks like the screen may be just displaying and closing the solution could be to have a cin statement after it. Code: #include <iostream.h> int main() { int i; cout << "Hello World!\n"; return 0; cin >>i; } I am not a mac expert guy but that could be one of the reason.
No, that didn't change anything. But I got this warning. I also got it when the code was the other way. I didn't think it mattered but here it is.
Use the following header then #include <iostream> using namespace std As you had hello.h I thought of giving the old one .h version.
Code: #include <iostream> using namespace std; int main() { cout << "Hello World!\n"; return 0; } Try running this. It tells the compiler to have the cout function from the standard namespace.
You have a number of things to consider. First, programs are designed to run to completion and terminate. If you don't want it to terminate before you can see the output, write something that requires user input. One example showed that, but it was AFTER THE RETURN, so it was never executed. Had you run the program from a command line, you would have seen the results. That's because the window is owned by the system and not by the app, so it just returns to the prompt, and doesn't close. Code: #include <iostream> using std::cout; using std::cin; int main (int argc, char *argv []) { cout << "Hello, World\n"; cin.get (); return 0; } With C++, don't use the old .h headers. Much of the material they reference has been deprecated. Using 'cout << "Hello, World\n";' is fine in most instances. Sometime you need to make sure the output is flushed to the screen (more complex, possibly multithreaded apps, for instance). In that case, use 'cout << "Hello, World" << endl;'. "using namespace std;" is contraindicated in all but the most trivial programs. You are very likely to wind up generating a duplicate symbol. Either use individual using statements (using std::cout or fully qualify the reference (std::cout << "blah". In some instances, where you have required previous input, the stream will already have information in it, so the cin.get () will be satisfied and not cause a pause. In those cases, clear the stream first with cin.sync () or cin.ignore ();
I also thought so when I posted but then having a statement after the return in main is not a good practice.
Nothing wrong with the code, as code. It's just that the cin is not effective (for keeping the window open). In those instances where the code is running in a window owned by the OS or the IDE and not the program, it isn't going to close until one closes it overtly, anyway.