-2

我是 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;
    }  
4

1 回答 1

1

编码的正确方法是消除system函数调用。使用特定于平台的 API 来实现功能。

你真的需要在每道题后清屏吗?
好像不太合适。有些人喜欢回顾以前的输出。此外,不适用于窗口系统。

年龄可以是负数吗?
C++ 具有int处理负数的类型,并且unsigned int该值将始终为零或正数。

在计算之前检查您的输入。
例如,如果出生年份大于当前年份,您将呈现负年龄。

从操作系统读取年份。
您不需要用户输入当前年份。大多数平台都有时钟和功能来获取当前时间。

于 2013-11-14T03:59:50.213 回答