我是 C++ 新手,我正在自学。所以我的问题是如何正确编码?对不起,如果这听起来像一个愚蠢的问题,或者如果它已经被回答。
问题是我正在尝试将我每天使用的一些批处理文件转换为 C++。我发现自己在使用 system(); 我读过的很多内容都是错误的。
在我开始养成坏习惯之前,我很想学习正确的方法。所以我问是否有人会以正确的方式重写这段代码并将其分解为细节。因此,C++ 新手可以更好地理解它以及为什么应该以某种方式完成它。
先感谢您。
#include <iostream>
int main ()
{
system("title My Age Is?");
system("mode con: cols=80 lines=25");
system("color 0a");
int myAge, yearBorn, yearNow;
// the year you where born in.
std::cout << "Enter the Year You where Born in:";
std::cin >> yearBorn;
system("cls");
// the year it is now.
std::cout << "Enter the Year it is Now:";
std::cin >> yearNow;
system("cls");
// the total of Now - Then = ?
myAge = yearNow - yearBorn;
// the output of Now - Then = ?
std::cout << "You are " << myAge << " Years Old" << std::endl;
std::cout << std::endl;
system("pause");
return 0;
}
好的,我想我越来越接近正确了。这就是我的目的。
#include <iostream>
#include <windows.h>
#include <ctime>
void cls( HANDLE hConsole )
{
COORD coordScreen = { 0, 0 }; // home for the cursor
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
// Get the number of character cells in the current buffer.
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
// Fill the entire screen with blanks.
if( !FillConsoleOutputCharacter( hConsole, // Handle to console screen buffer
(TCHAR) ' ', // Character to write to the buffer
dwConSize, // Number of cells to write
coordScreen, // Coordinates of first cell
&cCharsWritten ))// Receive number of characters written
{
return;
}
// Get the current text attribute.
if( !GetConsoleScreenBufferInfo( hConsole, &csbi ))
{
return;
}
// Set the buffer's attributes accordingly.
if( !FillConsoleOutputAttribute( hConsole, // Handle to console screen buffer
csbi.wAttributes, // Character attributes to use
dwConSize, // Number of cells to set attribute
coordScreen, // Coordinates of first cell
&cCharsWritten )) // Receive number of characters written
{
return;
}
// Put the cursor at its home coordinates.
SetConsoleCursorPosition( hConsole, coordScreen );
}
int main()
{
// system("color 0a");
int color = 0xa0;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
/* using the void cls function to clear the screen instead of system("cls");
but in this case i used it to clear the screen cache
as set the text and background colors from above system("color 0a");
using the void cls function */
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
cls(hStdout);
}
/* SetConsoleTitle(myAgeIs); to set the title of the console
instead of system("title My Age Is?"); */
char myAgeIs[] = "My Age Is?";
SetConsoleTitle(myAgeIs);
// the year you where born in.
int yearThen;
std::cout << "Enter the Year You where Born in:";
std::cin >> yearThen;
//using std::cin.get(); instead of system("pause");
std::cin.get();
// using the void cls function to clear the screen instead of system("cls");
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
cls(hStdout);
}
// used to get the system time now
time_t t = time(0);
struct tm * now = localtime( & t );
// the total of current time - yearThen = ?
int yourAge;
yourAge = (now->tm_year + 1900) - yearThen;
std::cout << "Your Age is " << yourAge;
//using std::cin.get(); instead of system("pause");
std::cin.get();
return 0;
}