clear screen.. is this the right way

This is a discussion on clear screen.. is this the right way within the C++ Programming forums, part of the General Programming Boards category; It works, but im just wondering if there is an easier / better way to do it. also, i used ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    132

    clear screen.. is this the right way

    It works, but im just wondering if there is an easier / better way to do it.

    also, i used the pause command, which waits for you to press a key to continue.

    is there any command to make it wait 5 secs and then continue automaticly?

    Code:
    // Guessing Game
    
    #include <iostream>
    #include <ctime> 
    
    using namespace std;
    
    int main()
    {
    int rNumber;  // Random number variable
    int Guess;    // Your input variable
    
    cout << "Welcome to Reece's, Guess the number game...\n\n\n\n\n";
    // random number
    srand(time(NULL));
    rNumber = rand() % 15 + 1;
    
    for ( ;; )
       {
          cout <<"Enter your answer: ";
          cin >> Guess;
    	  system("cls");
          if ( Guess == rNumber )
          {
             cout <<" CONGRATS\n\n\n\n";
    		 break;
          }
          if ( Guess > 0 && Guess < 15 )
          {
             cout << "Nope, wrong answer...\n";
    		 system("PAUSE");
    		 system("cls");
          }
          else
          {
             cout <<"ERROR! Enter a number above 0 but below 15.\n";
    		 system("PAUSE");
    		 system("cls");
          }
       }
    cout << "The answer is " << rNumber << endl;
    system("PAUSE");
    return 0;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    System is in fact the worst way possible to do lots of stuff. FAQ.

  3. #3
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    I'm sure some kind of sleep function of sorts is probably provided for use with Milliseconds as the parameter I bet.. though I'm too lazy to look it up to verify.

    Pointless side note: Maybe its just me, but infinite loops look better with a while statement rather than a for .. eh maybe I'm the only one.
    Last edited by Kurisu; 06-06-2006 at 12:58 AM.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    9

    good method

    Code:
    system("cls");
    
    Advantages:       None really.
    
    Disadvantages:  Spawns a process to do the dirty work for you.
    It's bad for security reasons. 
    For example, someone could map cls to another program, and yours would run it without realising.
    I use system("cls") and it seems to work perfectly.
    Does the word "someone" or a program that is
    already existing on your computer?
    Even so, what could be so bad about the system clearing the screen on accident?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I believe the bad thing is when you accidentally download some virus that calls itself cls.exe. You come along and have this (unnecessary and annoying to many experienced users) attempt to clear the screen, but instead start this virus and hell ensues...

    Or something like that.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    > Does the word "someone" or a program that is
    already existing on your computer?
    It could exist on your computer, or it could be some malicious person out to get you. Don't limit yourself to what is "possible" with programming. You must be paranoid to code safely. For instance, someone gets a hold of your new program and gets a decompiler. Now they can see your source code and all its loopholes.

    Now they know that any Windows user who bought your program has this particular system() loophole. And hackers will look for the slightest exploitations to bring whole systems down: they could attack users individually, or attack your whole network if your computers at work run this program, because all they have to do is replace cls.bat with a virus.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Windows provides Sleep(), declared in <windows.h>, and taking a parameter in milliseconds.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    20
    use clrscr(); for clearscreen (libary conio.h)

    Code:
    clock_t start_time;
    start_time = clock ();
    while ((clock()-start_time)<5*CLOCKS_PER_SEC) {}
    for time delay, fiddle with the 5 to change the secs
    I think that also use conio.h

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Whizza
    use clrscr(); for clearscreen (libary conio.h)

    Code:
    clock_t start_time;
    start_time = clock ();
    while ((clock()-start_time)<5*CLOCKS_PER_SEC) {}
    for time delay, fiddle with the 5 to change the secs
    I think that also use conio.h
    This could work if you compiler supports it. But generally it is a non standard function. We try not to encourage the use of non standard code here.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    20
    Quote Originally Posted by sand_man
    This could work if you compiler supports it. But generally it is a non standard function. We try not to encourage the use of non standard code here.
    Generally it applies to Windows based applications but an alternate console.h libary is normally used in its stead for those who dont support conio (normally macs)

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Quote Originally Posted by Whizza
    Code:
    clock_t start_time;
    start_time = clock ();
    while ((clock()-start_time)<5*CLOCKS_PER_SEC) {}
    That's going to KILL your CPU. Honestly, I've tried.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    20
    Quote Originally Posted by jafet
    That's going to KILL your CPU. Honestly, I've tried.
    Uhhuh in what sense???

  13. #13
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    In the sense that your CPU usage will go way up. This is not good if you only have 1 processor (100% CPU usage), or you're using a laptop (battery will deplete faster).

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    > Uhhuh in what sense???
    clock_t is just a long in most implementations, and CLOCKS_PER_SEC is a clock_t(1000). Now, your computer can do calculations about a million times faster than you (and I'm being nice) so that solution just busies the CPU with tons of math to do and a lot of return values being put on the stack as well.

    A cleaner way is probably just to pause for input with cin.get(); like usual.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CLear Screen Routine
    By AQWst in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2004, 07:24 PM
  2. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 02:41 AM
  3. Getting a clear screen...
    By Finchie_88 in forum C++ Programming
    Replies: 13
    Last Post: 09-03-2004, 05:38 PM
  4. Clear Screen Again!!!
    By trang in forum C Programming
    Replies: 3
    Last Post: 12-13-2003, 07:36 AM
  5. Yet another clear screen thread :D
    By kermit in forum Linux Programming
    Replies: 2
    Last Post: 11-20-2003, 04:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21