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.
/*
* 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.
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
// 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>
usingnamespace std;
int main()
{
string s;
cout << "Are you happy? " << flush;
getline( cin, s );
if (s.empty() || (toupper( s[ 0 ] ) != 'Y'))
return 1;
return 2;
}
// This program launches the first and gets its exit code
#include <cstdlib>
#include <iostream>
usingnamespace 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.
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
#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
}
elseif (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;
}
// 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