Clear the screen

Pages: 12
If it's ok, I made a header file for the Windows ClearScreen function you wrote.
I rewrote the code with the same variable names and such; just used my own formatting and comments.
It compiles in Code::Blocks with GCC; there's only one issue -- for some reason when you use it, it sets the console cursor to the X position below the position you were at. So if the cursor was at 1, 0; after calling ClearScreen() it would be at 2, 0, and so on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 * clearscreen.h [Version 0.0.1]
 * Copyright (C) 2009 Duoas Corporation. All rights reserved.
 */

#ifndef _CLEARSCREEN_H
#define _CLEARSCREEN_H

#include <windows.h>

void ClearScreen() {
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = {0, 0};

    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE)
        return;
    // Get the number of cells in the current buffer:
    if (!(GetConsoleScreenBufferInfo(hStdOut, &csbi)))
        return;

    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    // Fill the buffer with empty whitespace :P

    if (!(FillConsoleOutputCharacter(hStdOut,
                                    (TCHAR) ' ',
                                     cellCount,
                                     homeCoords,
                                     &count)));
        return;

    // Fill the buffer with the current colours and attributes:
    if (!(FillConsoleOutputAttribute(hStdOut,
                                     csbi.wAttributes,
                                     cellCount,
                                     homeCoords,
                                     &count)))
        return;
    // Now place the console cursor to the first position in the console:
    SetConsoleCursorPosition(hStdOut, homeCoords);
}

#endif


I got the copyright thing from Microsoft's in command prompt; I just added it in for a little joke.
Last edited on
So if I ShellExecute() an external program, can I get its return value?
Err... sure?
chrisname
When you start playing around with stuff don't be surprised when it breaks.
You've got an unconditional return on line 34, so the colors are never cleared and the cursor is never placed home.

Also, you should not be defining functions in header files. You should only prototype them:
clearscreen.h
1
2
3
4
5
6
7
8
9
// clearscreen.h
// #include <disclaimer.h>

#ifndef CLEARSCREEN_H
#define CLEARSCREEN_H

void ClearScreen();

#endif 

clearscreen.cpp
1
2
3
4
5
6
7
8
9
10
// clearscreen.cpp
// #include <disclaimer.h>

#include <windows.h>
#include "clearscreen.h"

void ClearScreen()
  {
  ...
  }

Now you must add "clearscreen.cpp" to your project so that it is compiled and linked also.


Kiana
The ShellExecute[Ex]() functions do not wait for the program to terminate before returning. They only start the program.

If that is all that you want, then using system() is enough.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// a.cpp

// This program asks a question.
// It returns an exit code of 1 if the user responds affirmatively.
// It returns an exit code of 2 if the user responds in any other way.

#include <cctype>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  string s;
  cout << "Are you happy? " << flush;
  getline( cin, s );
  if (s.empty() || (toupper( s[ 0 ] ) != 'Y'))
    return 1;
  return 2;
  }

b.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// This program launches the first and gets its exit code
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
  {
  int status = system( "a" );
  switch (status)
    {
    case 1:
      cout << "Good for you.\n";
      break;
    case 2:
      cout << "Sorry to hear it.\n";
      break;
    default:
      cout << "You must first compile a.cpp to a.exe\n";
    }
  return 0;
  }

Make sure to compile "b.cpp" to "b.exe".
And "a.cpp" to "a.exe".
Then run b.exe.

Hope this helps.
Ohh I added a semi-colon to the if-statement by accident :l

Now that's fixed, it works fine :)
how to create a program in turbo c++ that the output would be an asterisks running to each 4 sides of the screen?
1
2
3
4
5
6
7
for (int i = 0; i < bufferHeight; i++ {
    for (int j = 0; j < bufferWidth; i++) {
        cout << "*";
    }

    cout << "\n";
}

That would fill the buffer with asterisks spanning the entire width and height if bufferHeight and bufferWidth were so defined...
chrisname
When you start playing around with stuff don't be surprised when it breaks.
You've got an unconditional return on line 34, so the colors are never cleared and the cursor is never placed home.

Also, you should not be defining functions in header files. You should only prototype them:
clearscreen.h
1
2
3
4
5
6
7
8
9
10
// clearscreen.h
// #include <disclaimer.h>

#ifndef CLEARSCREEN_H
#define CLEARSCREEN_H

void ClearScreen();

#endif 

clearscreen.cpp

1
2
3
4
5
6
7
8
9
10
// clearscreen.cpp
// #include <disclaimer.h>

#include <windows.h>
#include "clearscreen.h"

void ClearScreen()
  {
  ...
  }


Now you must add "clearscreen.cpp" to your project so that it is compiled and linked also.


That doesn't work for me. I get an error saying
45:2 C:\Dev-Cpp\Money Converter\clearscreen.cpp #endif without #if
Make sure you didn't forget the #ifndef CLEARSCREEN_H near the top of the .h file.
Yeah, I got that. I copied it exactly.
Weird.
It should work with GCC.
Do you still have that file you sent me chrisname? That worked.

GCC is Dev-C++ right?
Hunter, post your exact code.
Money.cpp (main file)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#ifdef WIN32
#include <windows.h>
#endif
#include "console.h" // Colors the output text
#include "clearscreen.h" // Enables a "nice" looking format. Clears the screen where ever ClearScreen(); appears.

namespace con = JadedHoboConsole; // This enables the coloring of the output text. NEEDED

int main()
{
    using std::cout; // Makes cout << useful 
    using std::endl; // Makes endl; useful
    using std::cin; // Makes cin >> useful

  char choice; // Used later for storing user input
  
  do // Starts the do - while loop
  {
    ClearScreen(); // Clears the screen so the program doesn't repeat it self.
    int currency;
    float eurosdollars, dollarseuros, dollarsyen, yendollars, dollarsUKpounds, UKpoundsdollars, dollarkronor, kronordollar, dollarrubles, rublesdollar;

    /* Menu screen */
    cout << con::fg_white <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n" 
     << con::fg_blue <<"1. Dollars to Euros\t\t\t2. Euros to Dollars\n"
     << con::fg_green <<"\n3. Dollars to Yen\t\t\t4. Yen to Dollars\n"
	 << con::fg_red <<"\n5. Dollars to UK Pounds\t\t\t6. UK Pounds to Dollars\n"
	 << con::fg_white <<"\n7. Dollars to Sweden Kronor\t\t8. Sweden Kronor to Dollars\n"
	 << con::fg_magenta <<"\n9. Dollars to Russian Rubles\t\t10. Russian Rubles to Dollars\n"
     << con::fg_white <<"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
    cout << con::fg_yellow <<"\nPlease enter the number of the currency you want to convert: ";
    cin >> currency;

    switch (currency) 
    {
        case 1:
            cout << con::fg_blue <<"\nPlease enter the amount of United States Dollars you would like to convert to European Euros: ";
            cin >> eurosdollars;
            cout << con::fg_blue <<"\nYou have entered " << eurosdollars << " Dollars which is equal to " << eurosdollars*0.678518116 << " Euros." << endl;
            break;
        case 2:
            cout << con::fg_blue <<"\nPlease enter the amount of European Euros you would like convert to United States Dollars: ";
            cin >> dollarseuros;
            cout << con::fg_blue <<"\nYou have entered " << dollarseuros << " Euros which is equal to " << dollarseuros*1.4738 << " Dollars." << endl;
            break;
        case 3:
            cout << con::fg_green <<"\nPlease enter the amount of United States Dollars you would like to convert to Japanese Yen: ";
            cin >> dollarsyen;
            cout << con::fg_green <<"\nYou have entered " << dollarsyen << " Dollars which is equal to " << dollarsyen*95.71255 << " Yen." << endl;
            break;
        case 4:
            cout << con::fg_green <<"\nPlease enter the amount of Japanese Yen you would like to convert to United States Dollars: ";
            cin >> yendollars;
            cout << con::fg_green <<"\nYou have entered " << yendollars << " Yen which is equal to " << yendollars*0.0105652 << " Dollars." << endl;
            break;
        case 5:
            cout << con::fg_red<<"\nPlease enter the amount of United States Dollars you would like to convert to United Kingdom Pounds: ";
            cin >> dollarsUKpounds;
            cout << con::fg_red<<"\nYou have entered " << dollarsUKpounds << " Dollars which is equal to " << dollarsUKpounds*0.598787 << " United Kingdom Pounds." << endl;
            break;
        case 6:
            cout << con::fg_red<<"\nPlease enter the United Kingdom Pounds you would like to covert to United States Dollars: ";
            cin >> UKpoundsdollars;
            cout << con::fg_red<<"\nYou have entered " << UKpoundsdollars << " United Kingdom Pounds which is equal to " << UKpoundsdollars*1.67004 << " Dollars." << endl;
            break;
        case 7:
            cout << con::fg_white<<"\nPlease enter the amount of United States Dollars you would like to convert to Sweden Kronor: ";
            cin >> dollarkronor;
            cout << con::fg_white<<"\nYou have entered " << dollarkronor << " Dollars which is equal to " << dollarkronor*7.19434 << " Sweden Kronor." << endl;
            break;
        case 8:
            cout << con::fg_white <<"\nPlease enter the amount of Sweden Kronor you would like to convert to United States Dollars: ";
            cin >> kronordollar;
            cout << con::fg_white <<"\nYou have entered " << kronordollar << " Kronor which is equal to " << kronordollar*0.138998 << " United States Dollars." << endl;
            break;
        case 9:
            cout << con::fg_magenta <<"\nPlease enter the amount of United States Dollars you would like to convert to Russian Rubles: ";
            cin >> dollarrubles;
            cout << con::fg_magenta <<"\nYou have entered " << dollarrubles << " Dollars which is equal to " << dollarrubles*31.5650 << " Russian Rubles." << endl;
            break;
        case 10:
            cout << con::fg_magenta <<"\nPlease enter the amount of Russian Rubles you would like to convert to United States Dollars: ";
            cin >> rublesdollar;
            cout << con::fg_magenta <<"\nYou have entered " << rublesdollar << " Rubles which is equal to " << rublesdollar*0.0316807 << " United States Dollar." << endl;
            break;
       
       
    }
    if (currency > 10) // If user chooses a number greater than 10 screen clears and shows message
     {  
        ClearScreen();  
        cout << con::fg_white <<"\n\t\tPlease only choose a number from the list.";
        /* Shows message for 3 seconds */
        int seconds = 1;
        #ifdef WIN32
           Sleep(seconds*3000);
        #else
           sleep(seconds);
        #endif
        return main (); // Goes back to menu screen
     }  
    
    else if (currency < 10) // After converting ask if user wants to convert again
      
       cout << "\nDo you want to convert again? (Y/N) ";
       cin >> choice;

  } while(choice != 'n' && choice != 'N');

  cout << con::clr <<"\n\n\n\n\t\t\t\tGoodbye!\n";
   // Shows "Goodbye!" for 2 seconds.
  int seconds = 1;
#ifdef WIN32
  Sleep(seconds*2000);
#else
  sleep(seconds);
#endif
    
  // Quits the program  
  return 0;
}



Last edited on
clearscreen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// clearscreen.cpp
// #include <disclaimer.h>

#include <windows.h>
#include "clearscreen.h"


void ClearScreen()
  {
  HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = {0, 0};

    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    if (hStdOut == INVALID_HANDLE_VALUE)
        return;
    // Get the number of cells in the current buffer:
    if (!(GetConsoleScreenBufferInfo(hStdOut, &csbi)))
        return;

    cellCount = csbi.dwSize.X *csbi.dwSize.Y;

    // Fill the buffer with empty whitespace :P

    if (!(FillConsoleOutputCharacter(hStdOut,
                                    (TCHAR) ' ',
                                     cellCount,
                                     homeCoords,
                                     &count)))
        return;

    // Fill the buffer with the current colours and attributes:
    if (!(FillConsoleOutputAttribute(hStdOut,
                                     csbi.wAttributes,
                                     cellCount,
                                     homeCoords,
                                     &count)))
        return;
    // Now place the console cursor to the first position in the console:
    SetConsoleCursorPosition(hStdOut, homeCoords);

}
#endif




clearscreen.h
1
2
3
4
5
6
7
8
9
// clearscreen.h
// #include <disclaimer.h>

#ifndef CLEARSCREEN_H
#define CLEARSCREEN_H

void ClearScreen();

#endif  
Why is there an #endif on line 45 of "clearscreen.cpp"?

Hope this helps.
O.o Whoa. How did that get there?

It works marvelously. Thanks a lot Duoas.
:-)
Topic archived. No new replies allowed.
Pages: 12