为什么在这种情况下使用strcmp会使我的程序崩溃

Why does using strcmp in this situation crash my program?

本文关键字:我的 程序 崩溃 这种情况下 为什么 strcmp      更新时间:2023-10-16

我在使用strcmp时遇到困难。我在一个单独的else语句(未列出)中进行调用,它运行良好。这可能是内存问题吗?

while(inHere == 1)
{
    int numberOfOccupiedTables = 0;
    cout << "nSelect a table belown---------------nn";
    for(int i = 0; i < tables->size(); i++)
    {
        if(tables->at(i)->open == 0)
        {
            cout << "Table " << tables->at(i)->value << "n";
            numberOfOccupiedTables++;
        }
    }
    if(numberOfOccupiedTables == 0) 
        cout << "No customers found.n";
    else
    {
        cout << "(q to back out) Enter number of table: ";
        char* choice = (char*)malloc(sizeof(char)*256);
        fgets(choice,256,stdin);
        if(strcmp(choice, "qn") == 0)
            inHere = 0;
    }

如果fgets()由于到达文件末尾而失败,则不会向字符串添加null终止符。在执行strcmp()之前,请检查以确保它没有返回NULL

相关文章: