提问者:小点点

“>”:有符号/无符号不匹配。我怎么解决这个?


std::string text;
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Gets the console handle.
    PCONSOLE_SCREEN_BUFFER_INFO lpScreenInfo = new CONSOLE_SCREEN_BUFFER_INFO(); // Creates a pointer to the Screen Info pointing to a temporal screen info.
    GetConsoleScreenBufferInfo(hConsole, lpScreenInfo); // Saves the console screen info into the lpScreenInfo pointer.
    COORD NewSBSize = lpScreenInfo->dwSize; // Gets the size of the screen

    int choice{};
    
    do
    {
       
        if (NewSBSize.X > text.size())
        {

            int newpos = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.
            
            for (int i = 0; i < newpos; i++) std::cout << "ARES\n"; // Prints the spaces

            int newpos1 = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.
            
            for (int i = 0; i < newpos; i++) std::cout << "MENU\n"; // Prints the spaces
            
            int newpos2 = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.
            
            for (int i = 0; i < newpos; i++) std::cout << "Select one of the following options by pressing 1, 2 or 3:\n"; // Prints the spaces

            int newpos3 = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.
            
            for (int i = 0; i < newpos; i++) std::cout << "1. Activate Virus\n"; // Prints the spaces

            int newpos4 = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.
            
            for (int i = 0; i < newpos; i++) std::cout << "2. Program Information\n"; // Prints the spaces

            int newpos5 = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.
            
            for (int i = 0; i < newpos; i++) std::cout << "3. Exit\n"; // Prints the spaces

            std::cin >> choice; 
            
            switch (choice)
            {
                
            case 1:
                
                int newpos = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.

                for (int i = 0; i < newpos; i++) std::cout << "Infection Initiated. Press any key to continue . . .\n"; // Prints the spaces
                std::cin.ignore();
                system("CLS");
                Ares();
                
            case 2:
                
                int newposi = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.

                for (int i = 0; i < newposi; i++) std::cout << "Program Information loading. Press any key to continue . . .\n"; // Prints the spaces
                std::cin.ignore();
                system("CLS");
                ProgramInfo();
                
            case 3:
                
                int newposh = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.

                for (int i = 0; i < newposh; i++) std::cout << "Ending Operations. Press any key to continue . . .\n"; // Prints the spaces
                std::cin.ignore();
                system("CLS");
                menu();
                
            default:
                
                int newposa = ((NewSBSize.X - text.size()) / 2); // Calculate the number of spaces to center the specific text.

                for (int i = 0; i < newposa; i++) std::cout << "Invalid input! Press any key to continue . . .\n"; // Prints the spaces
                
            }

        }
        
    } 
    while (choice < 1 or choice > 3);

这是我得到错误的相关代码。如果你需要看更多我的代码,让我知道,我会添加它。

我在互联网上寻找一个解决方案,也发现了类似的问题,但没有一个修复工作做任何事情来解决问题。如有任何帮助,我将不胜感激


共1个答案

匿名用户

签名/未签名不匹配。我怎么解决这个?

从窄到宽。如果无符号类型较宽,则首先考虑负值。

如果相对宽度不确定,请先处理负值。

newsbsize.x显然是一个short,一种带符号的类型。

text.size()size_t,一种无符号类型。

如果newsbsize.x意外地为负数,则newsbsize.x>text.size()在逻辑上为false,因此在这种情况下会失败。

if (NewSBSize.X >= 0 && ....

然后将short转换为其未签名的对应方-或其未签名的提升对应方。这里的强制转换不对newsbsize.x进行值更改,并且根据两个无符号类型中较宽的一个进行以下无符号比较。

... (unsigned) NewSBSize.X > text.size()

总共

if (NewSBSize.X >= 0 && (unsigned) NewSBSize.X > text.size()) {

如果代码可以确保newsbsize.x>=0,那么就简化到下面。
警告:微优化是邪恶的。

if ((unsigned) NewSBSize.X > text.size()) {