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

loopfick

macrumors newbie
Original poster
Mar 22, 2005
6
0
Hi! I was wondering if anyone could tell me what I'm doing wrong with this program. Any help would be greatly appreciated!

// my first program in C++

#include <iostream>

using namespace std;

int main () {
* *cout << "Hello World!";
* *return 0;
}


Last login: Mon Mar 21 01:40:48 on console
Welcome to Darwin!
Chuck-Gishs-Computer:~ chuck$ cd /developer
Chuck-Gishs-Computer:/developer chuck$ g++ myprog.cpp
myprog.cpp:1: error: parse error before `{' token
myprog.cpp:1: error: stray '\' in program
myprog.cpp:1: error: stray '\' in program
myprog.cpp:1: error: stray '\' in program
myprog.cpp:1: error: stray '\' in program
myprog.cpp:2: error: stray '\' in program
myprog.cpp:2: error: stray '\' in program
myprog.cpp:2: error: stray '\' in program
myprog.cpp:2: error: stray '\' in program
myprog.cpp:2: error: stray '\' in program
myprog.cpp:2: error: stray '\' in program
myprog.cpp:2: error: stray '\' in program
myprog.cpp:2: error: parse error before `Courier'
myprog.cpp:3: error: stray '\' in program
myprog.cpp:3: error: stray '\' in program
myprog.cpp:3: error: stray '\' in program
myprog.cpp:3: error: stray '\' in program
myprog.cpp:3: error: parse error before `;' token
myprog.cpp:3: error: stray '\' in program
myprog.cpp:3: error: stray '\' in program
myprog.cpp:3: error: stray '\' in program
myprog.cpp:3: error: parse error before `;' token
myprog.cpp:4: error: stray '\' in program
myprog.cpp:4: error: stray '\' in program
myprog.cpp:4: error: stray '\' in program
myprog.cpp:4: error: stray '\' in program
myprog.cpp:4: error: parse error before `viewh9000'
myprog.cpp:4: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:5: error: stray '\' in program
myprog.cpp:7: error: stray '\' in program
myprog.cpp:7: error: stray '\' in program
myprog.cpp:7: error: stray '\' in program
myprog.cpp:15: error: stray '\' in program
myprog.cpp:15: error: stray '\' in program
myprog.cpp:15: error: stray '\' in program
myprog.cpp:16: error: stray '\' in program
myprog.cpp:16: error: stray '\' in program
myprog.cpp:16: error: stray '\' in program
myprog.cpp:16:14: missing terminating " character
myprog.cpp:16:14: warning: unknown escape sequence '\}'
Chuck-Gishs-Computer:/developer chuck$
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
loopfick said:
myprog.cpp:2: error: parse error before `Courier'
This line makes me think that you don't have it saved as plain text, but rather as Rich Text, or in some proprietary format like MS Word or whatever. Make sure you convert the file to PLAIN TEXT, and also give it the proper extension for c++ files so the compiler knows what it's looking at (I think it's .cpp). What app(s) are you using to edit and compile your code?
 

loopfick

macrumors newbie
Original poster
Mar 22, 2005
6
0
Textedit...

Thank you both for replying!

I am using Textedit in Mac OSX to write the code. It is saved in .rtf, but then I change it to .cpp

Next, I open terminal app, and use the g++ command to compile it...but it never works. I always get the same errors.

It's a little frustrating for someone who's trying to learn programming, but can't figure out how to start! :(

Thanks again...
 

loopfick

macrumors newbie
Original poster
Mar 22, 2005
6
0
more myprog.cpp

ChrisBrightwell said:
I'm curious -- what app did you use to write the .cpp file?

Try running this command:
more myprog.cpp

Then paste the result here.


OK...here's the result.

// my first program in C++

#include <iostream>

using namespace std;

int main () {
<CA> <CA>cout << "Hello World!";
<CA> <CA>return 0;
}
Chuck-Gishs-Computer:/developer chuck$
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
loopfick said:
Next, I open terminal app, and use the g++ command to compile it...but it never works. I always get the same errors.
You need to select Make Plain Text from the Format menu in TextEdit before you save. Changing the extension alone is not enough, as it's still saving invisible Rich Text formatting information in the file, which the compiler is seeing.

I would strongly recommend you get TextWrangler if you want to edit code. The app is free and is excellent for plain text editing. It handles indentation, will syntax-highlight your code, and has powerful search and replace features. TextEdit is fine for jotting down lists, but it's not a good code editor. Then later, when you want more control over managing your projects, install the developer tools that come with OS X and start using Xcode, which also has excellent syntax-highlighting, indentation, and text-wrap handling, and can do fancy things like suggesting completions of programming terms as you type them. Plus, it'll keep all of your project files together and will compile and run your app at the click of a single button.

EDIT: In TextEdit, open Preferences and uncheck "Append '.txt' Extension To Plain Text Files" before saving...if you really insist on using it to edit your code. But as I said, I highly recommend against it. I typed the app into TextEdit and got similar error messages. When I converted to plain text and made sure it did not have any weird extensions, it compiled without a hitch.
 

loopfick

macrumors newbie
Original poster
Mar 22, 2005
6
0
Xcode...

Thanks for replying! I will try Textwrangler.... :)

I tried using Xcode to compile the Hello World program, but it didn't work. Only the "Hello World" example file that they included worked. But it had alot of headers and such that I didn't understand.

So far, if I open a carbon project, erase all the headers in the main.whatever file, paste my beginner program in the editor then try to compile, it doesn't work.

Am I trying to learn c in an environment that does not run the first programs for beginners?
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
loopfick said:
I tried using Xcode to compile the Hello World program, but it didn't work. Only the "Hello World" example file that they included worked. But it had alot of headers and such that I didn't understand.

So far, if I open a carbon project, erase all the headers in the main.whatever file, paste my beginner program in the editor then try to compile, it doesn't work.

Am I trying to learn c in an environment that does not run the first programs for beginners?
Should work, but Carbon project type is probably not what you want. If you select New Project from the File menu, then select "C++ Tool" as the project type, you'll see it actually recreates your Hello World program almost identically, by default. "C++ Tool" creates a command-line project, just like you would (or can) run in the Terminal.app. The code goes in the main.cpp file, which is the first code executed when you run your app (in other words, main.cpp is the entry point of your app, where you can create other classes or tell your app to execute code in other files, but for simple apps you can just put the code right into main.cpp. You do need to keep the "int main (int argc, char * const argv[]) {" and "}" wrapper around your code. I'm assuming you're using the developer tools that shipped with Panther/10.3? I don't program C++ (I use C/Objective-C) but I did try it and it works. However, it might be easier for you to stick with the TextWrangler/g++/Terminal combo for now until you get used to it a bit. It is definitely a simpler setup than using Xcode, which has lots of stuff in it.
 

loopfick

macrumors newbie
Original poster
Mar 22, 2005
6
0
Thanks HiREZ!

This helps me a great deal. I am trying to learn C, in order to learn Objective-C. It just didn't seem like anything was working. I am following online tutorials right now. Thanks again!
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,629
Western US
loopfick said:
This helps me a great deal. I am trying to learn C, in order to learn Objective-C. It just didn't seem like anything was working. I am following online tutorials right now. Thanks again!
Ohhhhh well if that's the deal, stop using C++ :rolleyes: That'll really mess you up! In that case, "Standard Tool" is the Xcode project type you want for a straight-C (as opposed to C++) program, such as Hello, World. As before, you must keep your code wrapped in the template the main.c file creates, just put your code where it says "// insert code here..." (duh). Once you are ready to try Objective-C and Cocoa (in which you can use all your straight-C skills without problem), then you probably want the "Cocoa Application" or "Cocoa Document-based Application" project types. If compiling C in the Terminal, you want the "gcc" command (where you were using "g++"). If you hunt for tutorials, make sure you find one that is teaching you ANSI C and not C++, because even though they are related, it can really get confusing between the two of them. Should be tons of straight-C tutorials online.
 

loopfick

macrumors newbie
Original poster
Mar 22, 2005
6
0
Thank You!

I really appreciate this information! It seems like alot of people just take all of this for granted. None of the million tutorials on Xcode or C programming gave me any idea of what you just told me. I guess they figure you already know it, being a programmer and all...but for people who know next to nothing about programming?

Thanks again!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.