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

fumixene

macrumors newbie
Original poster
Nov 12, 2009
5
0
I am a new xcode programmer, but I have been programming in c and c++ for years. The following very simple program runs just find on my windows Borland compiler but it is an antiquated machine. I have tried a variety of different directories in front of iostream, no luck. I'm very frustrated.

#import <Cocoa/Cocoa.h>
#include <iostream> error: iostream: no such file or directory
using namespace std;
int main()
{
int var1; //define var1
int var2; //define var2
var1 = 20; //assign value to var1
var2 = var1 + 10; //assign value to var2
cout << “var1+10 is “; //output text cout
<< var2 << endl; //output value of var2
return 0;
}
 
Which version of the OS and Xcode?

Which project template did you use for the project?

What is the extension on your source file? Is it ".mm"?
 
Are you sure you chose the right language?

It looks like you started making a Obj-C project rather than a standard C++ one. Try again and remove the "#import" line.

Code:
#include <iostream>
using namespace std; 
int main() 
{ 
int var1; //define var1 
int var2; //define var2
var1 = 20; //assign value to var1 
var2 = var1 + 10; //assign value to var2 
cout << “var1+10 is “; //output text cout 
<< var2 << endl;	 //output value of var2 
return 0; 
}
 
I've tried removing the import line and I get exactly the same errors.

My operating system is 10.5.8 and my version of xcode is 3.1.3. It's the proper version for Leopard, I don't have the Snow Leopard version.

I don't remember the project template I used, which one should I have used?

My source code is in main.m.

Thanks so far for you help.

Kathy
 
You should choose a command line tool, of type c++ stdc++.

You're filename indicates that you chose a foundation tool. This will compile as Objective-C. If you rename the file to .mm it will compile as Objective-C++ and should work. A C++ file will have a .cpp extension.

-Lee
 
Thanks so much Lee, as I said I'm new to xcode. You're suggestion helped get rid of most of my errors.

I have two left though, now I get the following errors in my code:

//
// intvars.cpp
// demonstrates integer variables
#include <iostream>
using namespace std; int main()
{
int var1; //define var1
int var2; //define var2
var1 = 20; //assign value to var1
var2 = var1 + 10; //assign value to var2
cout << “var1+10 is “ << var2 << endl; //output text
error expected primary expression before 'OTHER' token
error expected ";" before 'OTHER' token

return 0;
}
 
cout << “var1+10 is “ << var2 << endl; //output text

The above statement contains curly double-quotes, as does the original post. Make sure they are straight double-quotes (0x22) in the actual source file.

Use the hexdump command in Terminal if you're not sure what the file really contains.

The following also appears to be an error in the original source post:
Code:
<< var2 << endl;	 //output value of var2
 
Thank you so much! I copied parts of this program from my textbook and thought that the weird looking quotes were xcode's version of quotes -- very stupid mistake since I have been programming in C for a long time and should have caught that one.

The second part was a typo. When I copied the code it was all on one line and you'll notice the cout was on the line above and I just pressed return at the wrong spot.

Again, thanks a lot. I now have finally compiled my first program in xcode.
 
The second part was a typo. When I copied the code it was all on one line and you'll notice the cout was on the line above and I just pressed return at the wrong spot.

You also added a semicolon:
Code:
cout << “var1+10 is “; //output text cout
 
cout << “var1+10 is “; //output text cout
<< var2 << endl; //output value of var2

This was my original code and of course I fixed the quotes. Isn't it correct syntax to write:

cout << "var1+10 is ";
cout << var2 << endl;

which was my original intention (you can see on the first line of the original that I accidently added a line return after the cout instead of before).

I think it's also correct to put:

cout << "var1+10 is "
<< var2 << endl;​

or:

cout << "var1+10 is " << var2 << endl;

They all compile and I get the same output, but is one better or preferred over the other?

Thanks.
 
Those all look like correct syntax to me. That's not my point, though.

You said that you "pressed return". My point was that you did more than press return, you also added a syntactically significant semicolon, which made the following line have incorrect syntax. It may not have been your intention to do so, but that is what you did, and I was simply pointing it out as a bug. Most bugs arise from the difference between intention and action.

As to reading a prior line's comment as a misplaced newline before 'cout', I didn't see it that way. I read it as you leaving out the word 'to', as in "output text to cout". That seems at least as plausible as a misplaced newline. We can't read your intention, only your actual code.

As to which one of your cout forms is better or preferred, it depends on what criteria you have for "better" or "preferred". Performance? Clarity? Coding standards compliance?

If you've been programming C and C++ for years, then you already know there may be an insignificant difference in performance. You'd also already know there may be differences in side-effects or thread-safety, but those would depend on the exact context. There isn't a uniformly better or preferred way.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.