I'm stumped.

JonB2004's Avatar
Light Poster
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.

Quote:
Building target "hello"
In function `int main()':
error: 'cout' was not declared in this scope
Here the code I was trying to compile.

Quote:
#include <hello.h>

int main()
{
cout << "Hello World!\n";
return 0;
}
Can anyone help me?
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
#include <hello.h>
This should be proper header file
#include <iostream.h>
0
JonB2004's Avatar
Light Poster
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
JonB2004's Avatar
Light Poster
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.

Quote:
#warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Use the following header then
#include <iostream>
using namespace std

As you had hello.h I thought of giving the old one .h version.
0
JonB2004's Avatar
Light Poster
Quote:
Originally Posted by shabbir
Use the following header then
#include <iostream>
using namespace std

As you had hello.h I thought of giving the old one .h version.

Could you explain that again? I don't quite understand.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
0
JonB2004's Avatar
Light Poster
It got rid of the warning message, but "HelloWorld" still hasn't come up on the screen. Damn.
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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 ();

Last edited by DaWei; 24Mar2007 at 15:00..