iccaros Posted April 5, 2005 Report Share Posted April 5, 2005 how do I clear the screen.. I would like my programs to run on a clean screen, is there a clear in c++, must be .. I know I just use clear in bash??thanks Quote Link to post Share on other sites
iccaros Posted April 5, 2005 Author Report Share Posted April 5, 2005 (edited) ok found it..system ("clear");but is there a more portable way as clear is linux but must be changed to cls for winders.??thanks Edited April 5, 2005 by iccaros Quote Link to post Share on other sites
jcl Posted April 5, 2005 Report Share Posted April 5, 2005 The only portable way is to blast enough newlines through the output stream to scroll away anything that might be there. Quote Link to post Share on other sites
iccaros Posted April 5, 2005 Author Report Share Posted April 5, 2005 hmmm.. ok.. well I'll just comment so I know where to change before I port.. thanks. Quote Link to post Share on other sites
jcl Posted April 5, 2005 Report Share Posted April 5, 2005 (edited) hmmm.. ok.. well I'll just comment so I know where to change before I port.. thanks.Heh. The idiomatic way to deal with this sort of thing is to use conditional compilation via preprocessor guards.void clear_screen(){#if defined WIN32 system("cls");#elif defined UNIX system("clear");#endif}The preprocessor will insert the code corresponding to the first branch of the conditional that tests true (or will remove the code for the false branches, depending on how you look at it). When the source file is compiled you have to compiler define which ever symbol fits the current platform. For Linux/GCC you would use something like$ gcc -DUNIX foo.cNormally you'd set up the makefile to handle platform selection. Still requires some work for each build (unless you use something like autoconf or hack together your own platform detector) but at least it's reduced to a single edit in an obvious enough place. Edited April 5, 2005 by jcl Quote Link to post Share on other sites
iccaros Posted April 5, 2005 Author Report Share Posted April 5, 2005 ok I follow.. I already have g++ foo.c -o fooso to add -DUNIX is no big deal... It bash script anyways enter program to be compiled.. exe name.. so on...I'm lazy and some time its easy to do ./compile foo.c and the bashscript does the rest.. so I could just add it to the script. I am happy I found the systme command as now my installer is makeing better progress.. Quote Link to post Share on other sites
iccaros Posted April 5, 2005 Author Report Share Posted April 5, 2005 ok .. I don't understand . I thought I would call it as a funcion.. am I missing something// This is a test program#include <iostream>#include <iomanip>void clear_screen(){#if defined WIN32 system("cls");#elif defined UNIX system("clear");#endif}using namespace std;int main (){ int grade, total, gradeCounter; double average; total = 0; gradeCounter = 0; clear_screen; cout << "Enter Grade or -1 to finish "; cin >> grade;while ( grade != -1) { total += grade; ++gradeCounter; cout << "Enter Grade or -1 to finish "; cin >> grade;}if ( gradeCounter != -1) { average = static_cast< double > ( total ) / gradeCounter; if ( average >= 60 ) cout << "you pass!! with a grade point average of: " << average << setprecision( 2 ) << fixed << endl; else cout << " You Failed!! with a Grade point average of: " << average << setprecision( 2 ) << fixed << endl;}else cout << "You did not enter any Grades!!!!" << endl;return 0;} Quote Link to post Share on other sites
iccaros Posted April 5, 2005 Author Report Share Posted April 5, 2005 (edited) I think posting makes my brain work add () after clear_screen...and change if ( gradeCounter != -1 ) to if (gradeCounter != 0)I will learn this... Edited April 5, 2005 by iccaros Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.