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

jdrm

macrumors newbie
Original poster
Jan 16, 2006
15
0
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 .
 

maxvamp

macrumors 6502a
Sep 26, 2002
600
1
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,719
1,894
Lard
Sending a formfeed should clear the Terminal window. If you don't know how to do that, you should look up escape sequences.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
jdrm said:
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.
maxvamp said:
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.
 
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.
 

maxvamp

macrumors 6502a
Sep 26, 2002
600
1
Somewhere out there
Soulstorm said:
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.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
maxvamp said:
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.
 

maxvamp

macrumors 6502a
Sep 26, 2002
600
1
Somewhere out there
Soulstorm said:
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,719
1,894
Lard
maxvamp said:
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! :D), 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.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
jdrm said:
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.
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
3
mrichmon said:
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.
 

Rocksaurus

macrumors 6502a
Sep 14, 2003
652
0
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
15,719
1,894
Lard
Rocksaurus said:
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.
 

weg

macrumors 6502a
Mar 29, 2004
888
0
nj
maxvamp said:
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? ;-)
 

Fender2112

macrumors 65816
Aug 11, 2002
1,135
386
Charlotte, NC
jdrm said:
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
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
weg said:
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.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
caveman_uk said:
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.
 

MacDonaldsd

macrumors 65816
Sep 8, 2005
1,005
0
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 ?
 

Fender2112

macrumors 65816
Aug 11, 2002
1,135
386
Charlotte, NC
MacDonaldsd said:
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. :D

On a side note: Does anyone know what the future plans are for Code Warrior?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.