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 Jan 16, 2006, 03:08 PM   #1
jdrm
macrumors newbie
 
Join Date: Jan 2006
c++ on mac os x

Wll I'm kinda new at c++ ..and even newer at using a mac....so here is my quetion:
How do you perform a c++ "clear screen" under mac os x?....under windows
system("cls") should get the job done so I thought of using system("clear)
but it returns me a "TERM no enviroment variable set"

cya

PD: I'm using xcode2 .
jdrm is offline   0 Reply With Quote
Old Jan 16, 2006, 03:20 PM   #2
maxvamp
macrumors 6502a
 
maxvamp's Avatar
 
Join Date: Sep 2002
Location: Somewhere out there
I am curious on why you are working in C++ on Mac. Unless you are working on UNIX code, you should probably focus on C/Obj-C.

Are you useing C++ for learning purposes?

As to the Question... it sounds like you are looking for a console command. Could you send a snippet of code where you are using this?

Max.

Last edited by maxvamp; Jan 16, 2006 at 03:48 PM.
maxvamp is offline   0 Reply With Quote
Old Jan 16, 2006, 03:46 PM   #3
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Sending a formfeed should clear the Terminal window. If you don't know how to do that, you should look up escape sequences.
bousozoku is offline   0 Reply With Quote
Old Jan 16, 2006, 04:21 PM   #4
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by jdrm
Wll I'm kinda new at c++ ..and even newer at using a mac....so here is my quetion:
How do you perform a c++ "clear screen" under mac os x?....under windows
system("cls") should get the job done so I thought of using system("clear)
but it returns me a "TERM no enviroment variable set"

cya

PD: I'm using xcode2 .
No matter what you use, note that the system(...) commands are intended to give commands to the system's command line. For example, the following program will open Disk Utility and Terminal.
Code:
#include <iostream>


int main(){
	system("Open -a \"disk utility\"");
	system ("Open -a terminal");
	return 0;
}
So these functions will work as if you have given them into the OS X terminal. But xCode is not designed to handle all the functions that the command line handles. So, when you are building your application with xCode, it will get you an error when you run it at the point system("clear"); is used. But if you build the application and run it as a command line in the finder, as it is intended to be ran (to do that, just find the newly-built application in the finder and open it) it will work as expected.

So, to sum this up: Don't worry about this error. You will get this error only when you use this command in xCode. When you run the application you built as a normal command-line application (in which the terminal will be used) everything is going to work fine.
Quote:
Originally Posted by maxvamp
I am curious on why you are working in C++ on Mac. Unless you are working on UNIX code, you should probably focus on C/Obj-C.
Why? He can build Carbon applications for OS X with ease if he's learning C++.
C++ and C are for Carbon applications
Obj-C is for Cocoa.

Last edited by Soulstorm; Jan 16, 2006 at 04:27 PM.
Soulstorm is offline   0 Reply With Quote
Old Jan 16, 2006, 04:54 PM   #5
AlmostThere
macrumors 6502a
 
Join Date: Feb 2005
Location: The royal throne of kings, the sceptred isle, the other Eden, the fortress built
AFAIK the Xcode pseudo-terminal doesn't have these capabilities (or more precisely, I don't know and haven't found out what type of terminal it is supposed to be).

You can work around this issue by selecting you executable in XCode in the Groups & Files side bar, clicking Info and then the Arguments tab. Here add an environment varaible called TERM with the value xterm-color.

You can play around with different terminal types, if you like. It won't clear your output though but if you might find a value of TERM that does (try Google). Obviously, not all escape codes are going to work, either, as these are also tied to the terminal type.

The alternative is to run your executable from Terminal.app rather than through XCode, which should work as expected. Again, AFAIK, you can't set XCode to use Terminal.app for I/O.
AlmostThere is offline   0 Reply With Quote
Old Jan 16, 2006, 05:43 PM   #6
maxvamp
macrumors 6502a
 
maxvamp's Avatar
 
Join Date: Sep 2002
Location: Somewhere out there
Quote:
Originally Posted by Soulstorm
Why? He can build Carbon applications for OS X with ease if he's learning C++.
C++ and C are for Carbon applications
Obj-C is for Cocoa.
Sorry, I was under the impression that Carbon was basically going away in the new Intel Macs. After a bit of research on ADC, I have been corrected and realize that if one had built OS 8 or OS 9 Carbon Apps, they will have to a bit of work to make them operate on Intel Macs, but it is possible.

In either case, I always keep the motto of "When in Rome....". jdrm could learn C++ on the Mac, but it is my belief that if he is looking to be the most efficient on the Mac platform, he would do well to learn the Obj-C flavor of an Object oriented C language . If jdrm is going after the marketable skill set, then C++ is a better one to pick, as he will be able to apply it to *NIX and Windows programming.

Max.
maxvamp is offline   0 Reply With Quote
Old Jan 17, 2006, 02:18 AM   #7
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by maxvamp
Sorry, I was under the impression that Carbon was basically going away in the new Intel Macs. After a bit of research on ADC, I have been corrected and realize that if one had built OS 8 or OS 9 Carbon Apps, they will have to a bit of work to make them operate on Intel Macs, but it is possible.

In either case, I always keep the motto of "When in Rome....". jdrm could learn C++ on the Mac, but it is my belief that if he is looking to be the most efficient on the Mac platform, he would do well to learn the Obj-C flavor of an Object oriented C language . If jdrm is going after the marketable skill set, then C++ is a better one to pick, as he will be able to apply it to *NIX and Windows programming.

Max.
Yup. He could learn OBJ-C, but it would be better to continue learning C++ as he does now. Just think about it: What good is OS X programming if you can't make proffessional programs using C++, one of the world's most famous and advanced language? It would be a tremendous mistake for apple. So I don't think there would be any problem.
Soulstorm is offline   0 Reply With Quote
Old Jan 17, 2006, 12:28 PM   #8
maxvamp
macrumors 6502a
 
maxvamp's Avatar
 
Join Date: Sep 2002
Location: Somewhere out there
Quote:
Originally Posted by Soulstorm
Yup. He could learn OBJ-C, but it would be better to continue learning C++ as he does now. Just think about it: What good is OS X programming if you can't make proffessional programs using C++, one of the world's most famous and advanced language? It would be a tremendous mistake for apple. So I don't think there would be any problem.

Ahh, You just hit directly my biggest complaint about OS X. I actually like the Cocoa framework better, but having to work with it with Obj-C and ( for me in any case ) it is a big pain in the butt. I am a C++ guy, and I have had some issues stemming from habit when trying to work in Obj-C. Carbon has in recent years been treated as the red headed step child by Apple and by the Book publishing community. This seems to be forcing the Obj-C language.
Luckily, Obj-C++ is starting to come more into the Apple vernacular


My ultimate concern is that, being a beginner, the original poster is not overwhelmed by the amount of knowledge needed, especially when trying to trying to move from one language to another.

I guess the question for him ( or her ) is... What is your long term goals for coding ( only OS X, or multi-platform ) .


Long and short... C++ is a good language to learn, but knowing what they plan on doing makes it impossible to determine what path is the best.

On one other note, I just finished the book "Beginning Mac OS X Programming". This is an excellent book for people beginning Mac Programming. It covers Carbon and Cocoa, C and Obj-C, and a generally good bit of well rounded knowledge. It may be a bit light weight for someone with medium to advanced programming knowledge. BookPool.com has it for 50% off.

Max.
maxvamp is offline   0 Reply With Quote
Old Jan 17, 2006, 01:13 PM   #9
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by maxvamp
Ahh, You just hit directly my biggest complaint about OS X. I actually like the Cocoa framework better, but having to work with it with Obj-C and ( for me in any case ) it is a big pain in the butt. I am a C++ guy, and I have had some issues stemming from habit when trying to work in Obj-C. Carbon has in recent years been treated as the red headed step child by Apple and by the Book publishing community. This seems to be forcing the Obj-C language.
Luckily, Obj-C++ is starting to come more into the Apple vernacular
...
Max.
A few people decided that because Cocoa wasn't traditional Apple technology that it was better. Carbon was quite an upgrade to the traditional Toolbox and works quite well, doing some things easily in C for which Windows programmers have to use C++ and a framework. Besides that, Cocoa and Carbon support each other. It's unlikely at this point that they can be unraveled. As long as someone isn't writing an application with a System 6-style wait loop (Hey Adobe! ), things should work quite well. Of course, using Cocoa almost guarantees that you can create a Universal Binary easily. System 6-style Carbon needs a lot of work to get things going.

Objective-C++ would be great if the rest of the gcc suites--not just Apple's--had it.
bousozoku is offline   0 Reply With Quote
Old Jan 17, 2006, 08:52 PM   #10
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by jdrm
Wll I'm kinda new at c++ ..and even newer at using a mac....so here is my quetion:
How do you perform a c++ "clear screen" under mac os x?....under windows
system("cls") should get the job done so I thought of using system("clear)
but it returns me a "TERM no enviroment variable set"

cya

PD: I'm using xcode2 .
Strictly, c++ does not provide any way to clear the screen. This is because clearing the screen is technically a terminal control operation and c++ does not have any built in terminal control operations.

Terminal control is very dependant on the underlying OS and platform. In a Unix environment (including OS X) nowdays you can be fairly certain that the underlying terminal will be emulating an xterm. This means that if you print the appropriate control codes to clear an xterm screen then it will probably work for you. In this case, the control codes would be printed by
printf("\33[2J"); (\33 prints the "escape" character).

As other posters have pointed out, the XCode console might not emulate an xterm and thus may not understand the above control codes. If this is the case then the above printf will not clear the XCode console, but will clear a terminal window when the application is run from the command line.

Your original attempt - using system("clear"); will work if you are running in the terminal and the TERM environment in the terminal is set correctly (eg to "xterm"). This system(...) operation is trapping out of your program to invoke the command "clear". Although this would work it is very inefficient since it requires the creation of a new process (running program) to do a simple operation.

Last edited by mrichmon; Jan 17, 2006 at 08:57 PM.
mrichmon is offline   0 Reply With Quote
Old Jan 17, 2006, 09:11 PM   #11
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by mrichmon
printf("\33[2J");
A full list of the control sequences you can use like this can be found at http://www.xfree86.org/current/ctlseqs.html

For reference, the \33[2J breaks down into
  • \33[ = Control Sequence Introducer (CSI on the linked web page, and shown in the C1 (8-Bit) Control Characters List)
  • 2J = CSI P s J (Erase in Display (ED) under the heading "Functions using CSI, ordered by the final characters" on the linked page). 2 corresponds to "Erase All"

You can build up a fairly sophisticated interface just by printing the control sequences directly to move the cursor around, clear the screen and erase and print in various ways. However, you are much, much, much better off investigating and using a standard library to do this sort of thing. One of the most common libraries to use is the Curses library which you can google for. I'm not sure if Curses has a C++ binding.

Under the covers, anything that controls the terminal is ultimately printing the control sequences to the terminal. Libraries like Curses provide easier ways for the programmer to work with the terminal.
mrichmon is offline   0 Reply With Quote
Old Jan 19, 2006, 02:32 AM   #12
Rocksaurus
macrumors 6502a
 
Rocksaurus's Avatar
 
Join Date: Sep 2003
Location: California
I'm pretty sure intel recently released the beta for their Mac compilers and they have a C++ compiler, so C++ could be a better option than Obj-C as far as intel optimization is concerned, when it comes time.
__________________
The coolest calendar on iOS - zoomendar.com
Rocksaurus is offline   0 Reply With Quote
Old Jan 19, 2006, 02:47 AM   #13
bousozoku
Moderator emeritus
 
Join Date: Jun 2002
Location: Gone but not forgotten.
Quote:
Originally Posted by Rocksaurus
I'm pretty sure intel recently released the beta for their Mac compilers and they have a C++ compiler, so C++ could be a better option than Obj-C as far as intel optimization is concerned, when it comes time.
Perhaps, but their compilers have a reputation for being better at benchmarks than real applications. Borland included the Intel compiler several years back, just to find that it was worse than their own in practical terms.
bousozoku is offline   0 Reply With Quote
Old Jan 19, 2006, 09:07 AM   #14
weg
macrumors 6502a
 
weg's Avatar
 
Join Date: Mar 2004
Location: nj
Quote:
Originally Posted by maxvamp
Unless you are working on UNIX code, you should probably focus on C/Obj-C.
C++ is widely used while Obj-C is not. Btw., is anybody still programming NewtonScript? ;-)
__________________
- weg
weg is offline   0 Reply With Quote
Old Jan 19, 2006, 09:50 AM   #15
SamMiller0
macrumors member
 
Join Date: Aug 2004
Location: San Jose, CA
Look into using the ncurses library, it will give you a portable way to clear the screen on a variety of platforms.
SamMiller0 is offline   0 Reply With Quote
Old Jan 19, 2006, 04:57 PM   #16
Fender2112
macrumors 6502a
 
Fender2112's Avatar
 
Join Date: Aug 2002
Location: Charlotte, NC
Send a message via AIM to Fender2112
Quote:
Originally Posted by jdrm
Wll I'm kinda new at c++ ..and even newer at using a mac....so here is my quetion:
How do you perform a c++ "clear screen" under mac os x?....under windows
system("cls") should get the job done so I thought of using system("clear)
but it returns me a "TERM no enviroment variable set"

cya

PD: I'm using xcode2 .
If your question has not been answered yet, read here: XCode Forums

The jest of it is this. The Xcode Run Log is more like a Terminal emulator. Because of this, there are some C++ commands that won't work. The code will compile with no errors, as it should. These commands work fine when run using Terminal. Click here for a Terminal Quick Start Guide
__________________
Really? Really, really!

Last edited by Fender2112; Jan 19, 2006 at 05:05 PM.
Fender2112 is offline   0 Reply With Quote
Old Jan 20, 2006, 02:56 AM   #17
caveman_uk
Guest
 
caveman_uk's Avatar
 
Join Date: Feb 2003
Location: Hitchin, Herts, UK
Quote:
Originally Posted by weg
C++ is widely used while Obj-C is not. Btw., is anybody still programming NewtonScript? ;-)
C++ is a pain in the ass and takes too long to write anything. Objective-C isn't that hard to learn - it's a hell of a lot easier to learn than C++. The Cocoa frameworks are what takes time to learn. To program on the mac you're probably going have to learn at least one new API. It comes down to whether you can be bothered to spend a small amount of time learning objective-C or would you rather make your life harder in the long run by sticking with C++ and using carbon.
caveman_uk is offline   0 Reply With Quote
Old Jan 20, 2006, 05:17 AM   #18
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by caveman_uk
C++ is a pain in the ass and takes too long to write anything. Objective-C isn't that hard to learn - it's a hell of a lot easier to learn than C++. The Cocoa frameworks are what takes time to learn. To program on the mac you're probably going have to learn at least one new API. It comes down to whether you can be bothered to spend a small amount of time learning objective-C or would you rather make your life harder in the long run by sticking with C++ and using carbon.
You can't avoid learning one new API. Wether this is cocoa, wether this is Carbon, it must be learnt.

Think it also this way: If one learns C++, the move to OBJ-C is easy.
Soulstorm is offline   0 Reply With Quote
Old Jan 23, 2006, 02:02 PM   #19
MacDonaldsd
macrumors 65816
 
MacDonaldsd's Avatar
 
Join Date: Sep 2005
Location: London , UK
What Apps to use for C++ ?

Ive been learning C++ for 4 months now at university but ive been using Visual Basics on Windows XP

So what apps would you recommended for The mac ?
MacDonaldsd is offline   0 Reply With Quote
Old Jan 23, 2006, 06:56 PM   #20
Fender2112
macrumors 6502a
 
Fender2112's Avatar
 
Join Date: Aug 2002
Location: Charlotte, NC
Send a message via AIM to Fender2112
Quote:
Originally Posted by MacDonaldsd
Ive been learning C++ for 4 months now at university but ive been using Visual Basics on Windows XP

So what apps would you recommended for The mac ?
Xcode.

On a side note: Does anyone know what the future plans are for Code Warrior?
__________________
Really? Really, really!
Fender2112 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
Mac 7950 on the ground, EFI works in 1,1 Mac Pro but drivers don't MacVidCards Mac Pro 127 Jun 27, 2014 09:25 PM
Migration assistant (Mac - Mac). New mac has mutiple drives, can't pick the right one Daftones MacBook Pro 4 Aug 29, 2013 09:32 AM
Linux and mac shared Data partition. (paragon extFS-mac, ntfs-mac, ntfs-3G ?) HiDeHo OS X 10.8 Mountain Lion 15 Jul 17, 2013 05:09 PM
Samsung announced 24" Mac compatible touchscreen monitor. Does it work on Mac 10.8.4? Warrenn Mac Peripherals 7 Jul 12, 2013 10:34 PM
Convert from Mac Pro to Mac Mini - high reliability backup solution? sdardens Mac Peripherals 3 Apr 7, 2013 10:03 PM

Forum Jump

All times are GMT -5. The time now is 03:35 AM.

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

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