我是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;
}
答案 0 :(得分:1)
正确的代码方法是消除system
函数调用。使用特定于平台的API来实现该功能。
每个问题后你真的需要清除屏幕吗?
似乎不太合适。有些人喜欢回顾之前的输出。此外,不适用于窗口系统。
年龄可能是负面的吗?
如果值始终为零或正数,则C ++具有int
类型来处理负数和unsigned int
。
在计算前检查您的输入。
例如,如果出生年份大于当年,您将呈现负年龄。
从OS读取年份。
您不需要用户输入当前年份。大多数平台都有时钟和功能来获得当前时间。