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

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

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

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

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

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...