if语句不起作用,直接跳到else部分。

4
//this is my source file, .cpp
#include <iostream>
#include <string>
#include "kingdom.h"
namespace westeros{
    void display(Kingdom pKingdom[], int kingdomElement, string KingdomName){
        cout << " ---------------- " << endl;
        cout << " Searching for kingdom " << KingdomName << " in westeros " << endl;
        for(int i=0; i<kingdomElement; i++){
            if (pKingdom[i].m_name == KingdomName){
                cout << " --------------------- " << endl;
                cout << KingdomName << ", population " << pKingdom[i].m_population << endl;
                cout << " --------------------- " << endl;
            }

            else{
                cout << " --------------------- " << endl;
                cout << KingdomName << " is not part of Westeros. " << endl;
                cout << " --------------------- " << endl;
            }
        }
    }
}
//this is my main file
#include <iostream>
#include "kingdom.h"
#include <string>
using namespace std;
using namespace westeros;

int main(void){
    int count = 0;
    Kingdom* pKingdoms = nullptr;
    pKingdoms = new Kingdom[count];
    display(pKingdoms, count, "Mordor");
    display(pKingdoms, count, "The_Vale");
    delete[]pKingdoms;
    pKingdoms = nullptr;
    return 0;
}

//this is my header file
#ifndef KINGDOM_H_
#define KINGDOM_H_
using namespace std;
namespace westeros{
    class Kingdom{
    public:
        char m_name[32];
        int m_population;  
    };
    void display(Kingdom pKingdom[], int kingdomElement, string KingdomName);
}
#endif

现在它会打印

摩尔多不是维斯特洛的一部分 摩尔多不是维斯特洛的一部分 摩尔多不是维斯特洛的一部分 摩尔多不是维斯特洛的一部分 摩尔多不是维斯特洛的一部分

谷地不是维斯特洛的一部分, 谷地,人口234567 谷地不是维斯特洛的一部分 谷地不是维斯特洛的一部分 谷地不是维斯特洛的一部分


1
你调用 new Kingdom[count],然后将分配的内存和 count 传递给 display。然后你访问元素 Kingdom[count](因为 kingdomElement 的值与 count 相同),这超出了你分配的内存范围,导致未定义行为。 - 1201ProgramAlarm
那么我该如何修复它以使其正常工作? - bob0306
4
pKingdoms = new Kingdom[count]; 这行代码中,count 此时为0,因此你创建了一个大小为0的数组。 - synchronizer
你在哪里向你的“Kingdom”数组添加元素? - greatwolf
请注意,此语法 new Kingdom[count]; 执行默认初始化,这意味着所有这些 Kingdom 元素都具有不确定的值,因为您没有构造函数(忽略 count 为0的事实)。 - greatwolf
显示剩余4条评论
2个回答

1
int count = 0;
Kingdom* pKingdoms = nullptr;
pKingdoms = new Kingdom[count];

这将创建一个元素数为0的数组。任何进一步的访问都将超出范围。
我建议您使用std::vector
std::vector<Kingdom> kingdoms(/*(good) count*/); 

作为您的 char m_name[32]; 的类型,使用 std::string

当我看到这行代码时:

cout << " Searching for kingdom " << KingdomName << " in westeros " << endl;

我希望看到的是类似以下的循环,而不是if。还需要一些代码来先填充数组中的一些名称。
在if => for版本之后:您应该打印“KingdomName不是Westeros的一部分。”在循环之后而不是内部,并且仅在未找到KingdomName时才打印。

@bob0306 嗯,你将if改为了for,这很好,但其他的都没变。 - O'Neil
等等,我不明白。在if之后应该改为for。修改后应该打印KingdomName << "不属于Westeros。这部分完全。这是否意味着将cout << Kingdomname << "不属于Westeros。" << endl; 改为 KingdomName << "不属于Westeros。"? - bob0306
@bob0306 这样好一些吗? - O'Neil
循环后面不在里面是什么意思? - bob0306
让我们在聊天中继续这个讨论 - bob0306
显示剩余3条评论

0

也许你只是忘记将The_Vale添加到pKingdoms数组中了。

所以,类似这样的语句将帮助你了解错误之处:

在主文件中,你可以像下面这样改进它,所有其他文件都没有问题 ;)

    //this is my main file
  #include <iostream>
  #include "kingdom.h"
  #include <string.h>
  using namespace std;
  using namespace westeros;

  int main(void){
      int count = 0;

      // Kingdom* pKingdoms = nullptr;
      // pKingdoms = new Kingdom[count];
      Kingdom* pKingdoms = new Kingdom[count];   // Might be a better choice,
                                          // as it reduces code.

      display(pKingdoms, count, "Mordor");      // pKingdoms doesn't have Mordor
      display(pKingdoms, count, "The_Vale");    // pKingdoms doesn't have The_vale

      // Here I add the 'The_Vale to the array 
      strcpy(pKingdoms[0].m_name, "The_Vale");
      pKingdoms[0].m_population = 1000;

      display(pKingdoms, count, "The_Vale");    // pKingdoms have The_vale

      delete[]pKingdoms;
      pKingdoms = nullptr;
      return 0;
  }

在编辑后,像这样在source.cpp中可能会有所帮助。

//this is my source file, .cpp
#include <iostream>
#include <string>
#include "kingdom.h"
namespace westeros{
    void display(Kingdom pKingdom[], int kingdomElement, string KingdomName){
      int flag = -1;
        cout << " ---------------- " << endl;
        cout << " Searching for kingdom " << KingdomName << " in westeros " << endl;

        for(int i=0; i<kingdomElement; i++)
            if (pKingdom[i].m_name == KingdomName)
                flag = i;

        if (flag != -1)
        {
            cout << " --------------------- " << endl;
            cout << KingdomName << ", population " << pKingdom[flag].m_population << endl;
            cout << " --------------------- " << endl;
        }

        else{
              cout << " --------------------- " << endl;
              cout << KingdomName << " is not part of Westeros. " << endl;
              cout << " --------------------- " << endl;
        }
    }
}

@bob0306 我没有在问题中看到任何代码修改。请编辑您的代码,以便我可以复现相同的输出。如果您认为我的答案是正确的,请将其标记为已接受。 - NVS Abhilash
我刚刚编辑了代码。你现在看到那个 for 循环了吗?它成功地输出了我想要的信息,即“Mordor 不是 Westeros 和 The_Vale 的一部分,人口 234567”。但是,它还会输出“The_Vale 不是 Westeros 的一部分”。 - bob0306
那么有没有办法摆脱不必要的消息? - bob0306
哦,我有一段C++代码,它是cout << "输入王国数量:"; c>> count; - bob0306
它无法工作,并且不断打印消息“摩多尔不是维斯特洛的一部分”。 - bob0306
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接