I'm stumped.

Discussion in 'C++' started by JonB2004, Mar 24, 2007.

  1. JonB2004

    JonB2004 New Member

    Joined:
    Mar 24, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    #include <hello.h>
    This should be proper header file
    #include <iostream.h>
     
  3. JonB2004

    JonB2004 New Member

    Joined:
    Mar 24, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  5. JonB2004

    JonB2004 New Member

    Joined:
    Mar 24, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    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.

     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Use the following header then
    #include <iostream>
    using namespace std

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

    JonB2004 New Member

    Joined:
    Mar 24, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0

    Could you explain that again? I don't quite understand.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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.
     
  9. JonB2004

    JonB2004 New Member

    Joined:
    Mar 24, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    It got rid of the warning message, but "HelloWorld" still hasn't come up on the screen. Damn.
     
  10. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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: Mar 24, 2007
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    :eek:
     
  12. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0

    Hey Shabbir, the code you works correct. then whats problem???
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I also thought so when I posted but then having a statement after the return in main is not a good practice.
     
  14. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  15. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    PLease elaborate that. I was unable to understand...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice