Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kylesow

macrumors newbie
Original poster
Apr 21, 2004
3
0
Collingwood, Ontario
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.
 

titaniumducky

macrumors 6502a
Nov 22, 2003
593
0
kylesow said:
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.

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.
 

vasaz

macrumors member
Mar 23, 2004
51
0
MN
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.
 

superbovine

macrumors 68030
Nov 7, 2003
2,872
0
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.
 

wadetemp

macrumors newbie
Apr 14, 2003
11
0
Boise
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,689
1,854
Lard
kylesow said:
A blank output window shows up.

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.
 

MoparShaha

Contributor
May 15, 2003
1,646
38
San Francisco
dontmakemehurtu said:
I don't know if C++ has evolved lately, but...

Java uses cout<<, C++ does not

C++ uses a system call, System.out.print or System.out.println

and enclose your output in parenthesis and quotes and end with a semicolon

System.out.println ("C++.....");

Don't take me word for it, however. I haven't programmed in years.
You've got the two swapped. C++ uses cout<< and java uses system.out.println() ;)

EDIT: I see wadetemp beat me to it!
 

csubear

macrumors 6502a
Aug 22, 2003
613
0
you need to go to the debug menu and hit consol window, or stdout or something like that
 

bux

macrumors regular
Apr 22, 2004
119
0
Sweden
kylesow said:
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.

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;
}
 

mateybob

macrumors member
Apr 18, 2004
76
0
Sydney
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?
 

Bear

macrumors G3
Jul 23, 2002
8,088
5
Sol III - Terra
mateybob said:
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?
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.
 

bux

macrumors regular
Apr 22, 2004
119
0
Sweden
Bear said:
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.
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).
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
Bear said:
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.

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'.
 

skyfex

macrumors newbie
Mar 18, 2004
26
0
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 ;)
 

Palad1

macrumors 6502a
Feb 24, 2004
647
0
London, UK
skyfex said:
Just for the sake of it.. try using the C style printf();

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

don't you need to call fflush(STDOUT) as well to be 100% sure absolutely guaranteed that the stream will be written ?
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,689
1,854
Lard
mateybob said:
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?

Even with otherwise working C, C++, or Objective-C that showed results at the shell prompt, Project Builder rarely showed anything.
 

encro

macrumors 6502
May 6, 2002
451
1
bendigo.victoria.au
kylesow said:
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.

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?
 

ElectricSheep

macrumors 6502
Feb 18, 2004
498
4
Wilmington, DE
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?
 

encro

macrumors 6502
May 6, 2002
451
1
bendigo.victoria.au
bousozoku said:
Even with otherwise working C, C++, or Objective-C that showed results at the shell prompt, Project Builder rarely showed anything.

I haven't experienced this at all in many versions of Project Builder or Xcode 1.1

Possibly machine specific?
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,689
1,854
Lard
encro said:
I haven't experienced this at all in many versions of Project Builder or Xcode 1.1

Possibly machine specific?

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.
 

Doctor Q

Administrator
Staff member
Sep 19, 2002
39,782
7,514
Los Angeles
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.