C++ on Xcode problem

Discussion in 'Mac Apps and Mac App Store' started by kylesow, Apr 21, 2004.

  1. kylesow macrumors newbie

    Joined:
    Apr 21, 2004
    Location:
    Collingwood, Ontario
    #1
    I'm trying to compile this C++ program on Xcode

    #include <iostream>
    using namespace std;
    int main()
    {
    cout << "C++ is power programming.";
    return 0;
    }

    the text "C++ is power programming" is suppose to be displayed, there are no errors or warnings but no text shows up. Please help I'm new to programming.
     
  2. titaniumducky macrumors 6502a

    titaniumducky

    Joined:
    Nov 22, 2003
    #2
    I program in Java and Perl, but I know some people who showed me a little C++. What is see so far is that you forgot to end your cout statement with the two opposite brackets after the end quote. So:

    cout << "C++ is power programming.">>;

    I can't guarantee this will work.
     
  3. vasaz macrumors member

    Joined:
    Mar 23, 2004
    Location:
    MN
    #3
    I can't see anything wrong, and tried it out through terminal (using g++) since I don't have access to Xcode at the moment, and it worked just as it should.
     
  4. superbovine macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #4
    its works for me.

    i used g++ test.cpp -o t

    then ./t from terminal

    if it isn't compiling xcode there maybe a config issue.
     
  5. wadetemp macrumors newbie

    Joined:
    Apr 14, 2003
    Location:
    Boise
    #5
    works in xcode for me

    titaniumducky's suggestion won't compile... >> is not appropriate in that line. Your source is definitely correct. I'm guessing the reason you're not seeing output is there's a configuration issue with XCode on your machine. For reference here's the steps I took with XCode 1.1:

    1) File->New Project->C++ Tool
    2) Chose a name and save location for the new project
    3) Whent he project window came up, I replaced the code in main.cpp with what you provided
    4) Clicked the Build And Run button in main.cpp's document window (it will ask you to save changed files if you haven't already)
    5) The Run window appears, which shows standard output by default, which is:
    C++ is power programming.
    testproject has exited with status 0.

    Are you seeing any output window? It's possible your output window's showing standard error instead, although I don't know if that's an option you can specify with XCode. Try changing your source to use cerr instead of cout to see if that's what's happening... ultimately you'll need to find out how to get XCode to show your output, but it would answer that question anyway.
     
  6. kylesow thread starter macrumors newbie

    Joined:
    Apr 21, 2004
    Location:
    Collingwood, Ontario
  7. wadetemp macrumors newbie

    Joined:
    Apr 14, 2003
    Location:
    Boise
    #8
    Well other than the fact that you have it completely backwards, you are right. :)
     
  8. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #9
    Xcode's predecessor IDE, Project Builder, had problems with capturing the output from programmes which would normally be executed at the shell prompt. Apparently, it hasn't totally been fixed or you don't have all of the patches applied to it.
     
  9. MoparShaha macrumors 68000

    MoparShaha

    Joined:
    May 15, 2003
    Location:
    San Francisco
    #10
    You've got the two swapped. C++ uses cout<< and java uses system.out.println() ;)

    EDIT: I see wadetemp beat me to it!
     
  10. csubear macrumors 6502a

    csubear

    Joined:
    Aug 22, 2003
    #11
    you need to go to the debug menu and hit consol window, or stdout or something like that
     
  11. bux macrumors regular

    Joined:
    Apr 22, 2004
    Location:
    Sweden
    #12
    Did you get this solved?
    I have had simular problems in some of my projects, I found out that cout may need the buffer flushed. To make it easy, std::endl can do that for you (put a new line to the text and then it flushes the stream).

    So test this:
    #include <iostream>

    using namespace std;

    int main()
    {
    cout << "C++ is power programming" << endl;
    return 0;
    }
     
  12. mateybob macrumors member

    Joined:
    Apr 18, 2004
    Location:
    Sydney
    #13
    firstly... ignore the things said by half these people.. they have seriously no idea...
    i think you definately need to try what bux said... <<endl; forces the output stream to be flushed and its good practice to use it on eery cout... but i would have thought the stream would be flushed when the program ends anyway?
     
  13. bux macrumors regular

    Joined:
    Apr 22, 2004
    Location:
    Sweden
    #14
    Me too, but I'm not sure.
     
  14. Bear macrumors G3

    Joined:
    Jul 23, 2002
    Location:
    Sol III - Terra
    #15
    Since endl also adds a new line, that could be what fixes it and not necesssarily the forced flush.

    If endl fixes the issue you could aloas try addong a newline )"\n") into your output string and remove the endl and see if that works also.
     
  15. bux macrumors regular

    Joined:
    Apr 22, 2004
    Location:
    Sweden
    #16
    Well, "\n" will most probably not fix it because you really need to flush the stream to be sure that your message will be shown (make it a habbit when you use cout).
     
  16. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #17
    There is a difference between "\n" and endl, in that endl forces a flush and "\n" doesn't. For most things this isn't important, but in general it's best to use << endl if writing to console or when writing small outputs and to use "\n" when writing large amounts of output. If a program is supposed to write lots of lines of output, I've seen performance-increase of 3000% just by replacing << endl with << '\n'.
     
  17. skyfex macrumors newbie

    Joined:
    Mar 18, 2004
    #18
    Just for the sake of it.. try using the C style printf();

    printf("C++ is power programming.\n");
    return 0;

    This works fine for me.. even compiling as an Carbon app i get an output window. I sometimes use it for debugging. I like this method better too.. cout << just looks messy ;)
     
  18. Palad1 macrumors 6502a

    Palad1

    Joined:
    Feb 24, 2004
    Location:
    London, UK
    #19
    don't you need to call fflush(STDOUT) as well to be 100% sure absolutely guaranteed that the stream will be written ?
     
  19. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #20
    Even with otherwise working C, C++, or Objective-C that showed results at the shell prompt, Project Builder rarely showed anything.
     
  20. encro macrumors 6502

    encro

    Joined:
    May 6, 2002
    Location:
    bendigo.victoria.au
    #21
    I have that book also, not a bad book either.
    (C++ A Beginner's Guide - Herbert Schildt ISBN: 0-07-219467-7)

    Your code is perfectly fine and will display without a problem and its probably very simple to fix. I think everyone is getting sidetracked and its just a misunderstanding of the xcode build process.

    Basics:
    1. Are you selecting C++ Tool from the project assistant?
    1. Are you clicking build and run and not just building it??? Cmd-R
    2. Are you sure your looking at your xcode run log??? Cmd-Shift-R to bring it to the front.
    3. If all else fails, open the build folder in your project and drag the executable into the terminal and try it from there. Is it working?
     
  21. ElectricSheep macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #22
    It seems to work just fine with XCode 1.1 on my Powerbook. Are you doing a "Build and Run" from within XCode? Are you doing just a "Run"? Are you doing a "Build and Debug" or a "Debug" from within XCode?
     
  22. encro macrumors 6502

    encro

    Joined:
    May 6, 2002
    Location:
    bendigo.victoria.au
    #23
    I haven't experienced this at all in many versions of Project Builder or Xcode 1.1

    Possibly machine specific?
     
  23. bousozoku Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #24
    Then, it's machine-specific to two very different machines (B&W G3/400 and dual G4/800) and quite a few others that I don't own. You can find various other threads here concerning this very same problem.

    I've gotten output out of PB possibly 1 of 100 times tried. If I had just started programming, I would fault myself, but after over 22 years of development experience and nearly 20 years of C, I have a couple of clues. :D I believe that Xcode worked much better but not enough to keep me from executing the programme at the shell prompt. It's extremely frustrating that they can't get it right but I'm glad it works for you.
     
  24. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #25
    Flushing cout only affects when you see the output, not if you see it. Files are closed when a program ends, and buffers are flushed when a file is closed. Flushing won't make any difference in the (eventual) output display.

    Flushing is useful in cases that don't apply to your test, such as:

    1. Interactive command-line programs. When you output a prompt or a warning or other message, you want to make sure it reaches the user before the program goes on.

    2. Real-time reports of what a program is doing. For example, debugging messages written to a log file should be flushed so that someone monitoring the log file (as the program is running) will have up-to-date information.

    3. Crash-proofing. Until a buffer is flushed, the output it contains is suseptible to loss if the program (or system) crashes. Once it is flushed, it is up to the operating system to send it to its destination. In the case of a disk file, delayed writes (an optimization technique) can still leave it vulnerable, but journaling (a safety technique) can protect it. Crash-proofing your output is sometimes important, such as writing a message about an important action, i.e., a financial transaction completed.
     

Share This Page