Sponsored By

Sign in to follow this  
iccaros

Another Simple C++ Question

Recommended Posts

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

Share this post


Link to post
Share on other sites

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 by iccaros

Share this post


Link to post
Share on other sites

The only portable way is to blast enough newlines through the output stream to scroll away anything that might be there.

Share this post


Link to post
Share on other sites

hmmm.. ok.. well I'll just comment so I know where to change before I port.. thanks.

Share this post


Link to post
Share on other sites
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.c

Normally 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 by jcl

Share this post


Link to post
Share on other sites

ok I follow..

I already have g++ foo.c -o foo

so 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..

Share this post


Link to post
Share on other sites

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;
}

Share this post


Link to post
Share on other sites

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 by iccaros

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this