C++ '&燃气轮机';:有符号/无符号不匹配。我该如何解决这个问题? std::字符串文本; 句柄h=GetStdHandle(标准输出句柄); HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);//获取控制台句柄。 PCONSOLE_SCREEN_BUFFER_INFO lpscreen INFO=新控制台_SCREEN_BUFFER_INFO();//创建指向临时屏幕信息的屏幕信息指针。 GetConsoleScreenBufferInfo(hConsole,lpScreenInfo);//将控制台屏幕信息保存到lpScreenInfo指针中。 COORD NewSBSize=lpScreenInfo->dwSize;//获取屏幕的大小 int选择{}; 做 { if(NewSBSize.X>text.size()) { int newpos=((NewSBSize.X-text.size())/2);//计算将特定文本居中的空格数。 对于(inti=0;itext.size()在逻辑上为false,因此学究式地失败 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);

C++ '&燃气轮机';:有符号/无符号不匹配。我该如何解决这个问题? std::字符串文本; 句柄h=GetStdHandle(标准输出句柄); HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);//获取控制台句柄。 PCONSOLE_SCREEN_BUFFER_INFO lpscreen INFO=新控制台_SCREEN_BUFFER_INFO();//创建指向临时屏幕信息的屏幕信息指针。 GetConsoleScreenBufferInfo(hConsole,lpScreenInfo);//将控制台屏幕信息保存到lpScreenInfo指针中。 COORD NewSBSize=lpScreenInfo->dwSize;//获取屏幕的大小 int选择{}; 做 { if(NewSBSize.X>text.size()) { int newpos=((NewSBSize.X-text.size())/2);//计算将特定文本居中的空格数。 对于(inti=0;itext.size()在逻辑上为false,因此学究式地失败 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);,c++,C++,有符号/无符号不匹配。如何修复此问题 从窄到宽强制转换。如果无符号类型更宽,则首先考虑负值 如果相对宽度不确定,则首先处理负值 NewSBSize.X是一种short,一种签名类型 text.size()是一种无符号类型 如果NewSBSize.X意外为负值,NewSBSize.X>text.size()在逻辑上为false,因此学究式地失败 std::string text; HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); HANDL

有符号/无符号不匹配。如何修复此问题

从窄到宽强制转换。如果无符号类型更宽,则首先考虑负值

如果相对宽度不确定,则首先处理负值


NewSBSize.X
是一种
short
,一种签名类型

text.size()
是一种无符号类型

如果
NewSBSize.X
意外为负值,
NewSBSize.X>text.size()
在逻辑上为false,因此学究式地失败

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);
然后将
short
转换为其无符号对应项-或其无符号提升对应项。此处的强制转换将no值更改为
NewSBSize.X
,并基于两种无符号类型中较宽的类型进行以下无符号比较

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

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

如果代码可以确保
NewSBSize.X>=0
,则简化为以下内容。
警告:微优化是邪恶的

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

请发布准确的错误消息。您找到了哪些解决方案,以便我们了解哪些不起作用?请同时阅读。实际阅读。然后执行。请。我假设
NewSBSize.X
int
std::string().size()
返回
大小\u t
,这可能是问题的根源。由于您既没有发布相关的定义,也没有发布确切的错误消息,所以我只能猜测。
if ((unsigned) NewSBSize.X > text.size()) {