Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 22, 2005, 02:56 AM   #1
loopfick
macrumors newbie
 
Join Date: Mar 2005
Hello World problems in C

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$
loopfick is offline   0 Reply With Quote
Old Mar 22, 2005, 03:12 AM   #2
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
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.
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old Mar 22, 2005, 03:14 AM   #3
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by loopfick
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?
HiRez is offline   0 Reply With Quote
Old Mar 22, 2005, 12:54 PM   #4
loopfick
Thread Starter
macrumors newbie
 
Join Date: Mar 2005
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 is offline   0 Reply With Quote
Old Mar 22, 2005, 01:15 PM   #5
loopfick
Thread Starter
macrumors newbie
 
Join Date: Mar 2005
more myprog.cpp

Quote:
Originally Posted by ChrisBrightwell
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$
loopfick is offline   0 Reply With Quote
Old Mar 22, 2005, 01:16 PM   #6
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by loopfick
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.

Last edited by HiRez; Mar 22, 2005 at 01:54 PM.
HiRez is offline   0 Reply With Quote
Old Mar 22, 2005, 01:50 PM   #7
loopfick
Thread Starter
macrumors newbie
 
Join Date: Mar 2005
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?
loopfick is offline   0 Reply With Quote
Old Mar 22, 2005, 02:07 PM   #8
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by loopfick
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.

Last edited by HiRez; Mar 22, 2005 at 02:12 PM.
HiRez is offline   0 Reply With Quote
Old Mar 22, 2005, 02:13 PM   #9
loopfick
Thread Starter
macrumors newbie
 
Join Date: Mar 2005
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!
loopfick is offline   0 Reply With Quote
Old Mar 22, 2005, 02:28 PM   #10
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
Quote:
Originally Posted by loopfick
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++ 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.

Last edited by HiRez; Mar 22, 2005 at 02:32 PM.
HiRez is offline   0 Reply With Quote
Old Mar 22, 2005, 04:56 PM   #11
loopfick
Thread Starter
macrumors newbie
 
Join Date: Mar 2005
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!
loopfick is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
First World Problems jondunford Community Discussion 21 Jan 31, 2014 06:49 PM
All iPads: Damn, can't decide which to get... First world problems, I know Sangdushi iPad 11 Oct 22, 2013 07:56 PM
RMBP 15" - Problems problems and more problems rmwebs MacBook Pro 14 Sep 30, 2013 04:22 AM

Forum Jump

All times are GMT -5. The time now is 08:22 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC