为什么我的代码不能与G++;编译程序? 我在VisualStudioC++中编写了代码,并将其转入G++编译器。虽然没有编译错误,但我在Visual Studio上运行的代码现在在执行新编译器时无法在新编译器上运行:问题是我的程序在没有格式错误时会报告格式错误

为什么我的代码不能与G++;编译程序? 我在VisualStudioC++中编写了代码,并将其转入G++编译器。虽然没有编译错误,但我在Visual Studio上运行的代码现在在执行新编译器时无法在新编译器上运行:问题是我的程序在没有格式错误时会报告格式错误,c++,g++,C++,G++,以下是关注的功能: void checkFormat(char *token, bool &checkNum, bool &checkString) { int nums = 0; //tracks # of digits in the token int chars = 0; //tracks # of letters in the token for(int i = 0; i < strlen(token); i++) //checks for

以下是关注的功能:

void checkFormat(char *token, bool &checkNum, bool &checkString)
{
    int nums = 0; //tracks # of digits in the token
    int chars = 0; //tracks # of letters in the token

    for(int i = 0; i < strlen(token); i++) //checks for formatting (must be either digits or letters)
    {
        if(isalpha(token[i]))
            chars++;
        else if(isdigit(token[i]))
            nums++;
        else
        {
            i = strlen(token);
            checkNum = false;
            checkString = false;
        }

        if (i == strlen(token) - 1)
        {
            if (nums > chars && chars == 0)
                checkNum = true;
            else if (chars > nums && nums == 0)
                checkString = true;
            else
            {
                checkNum = false;
                checkString = false;
            }
        }
    }
}
void checkFormat(char*token、bool&checkNum、bool&checkString)
{
int nums=0;//跟踪令牌中的#个数字
int chars=0;//跟踪标记中字母的#
for(int i=0;ichars&&chars==0)
checkNum=true;
else if(chars>nums&&nums==0)
checkString=true;
其他的
{
checkNum=false;
checkString=false;
}
}
}
}

当我在这行中使用这个语句:“SAL21030”出于某种原因,它在SAL上工作,在210上工作,但在30上失败。对于30,checkNum保持为false而不是true。有什么想法吗?

好吧,假设两个编译器都知道自己的工作。。。然后是环境或图书馆的不同。也许Linux或cygwin上的输入附带了附加到“30”的额外字符(NL?CR?),这使其无效。您可以尝试gdb,也可以返回到良好的ol'printf调试;-)。嗨,彼得,谢谢你的回复!我试过GDB,但它什么也没逮住。。我把我的代币打印出来,然后交进去,当时还是30。我还打印了120张,也只有120张。如果它适用于120,它应该适用于30,我不知道发生了什么,我的作业将在2-3小时后到期-尝试打印循环中的第一个字符。由于我使用strtok(NULL,“”)将这些字符转换为令牌,30是否也适用,因为这是行的末尾,还有换行符吗?所以令牌应该是30而不是30?没关系,我只是检查了一下,它什么也没做。这到底是怎么回事!?