if 语句不起作用并被跳到 else 部分

if statement is not working and is skipped to else part

本文关键字:else 部分 语句 不起作用 if      更新时间:2023-10-16
//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

现在它打印

魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分魔多不是维斯特洛的一部分

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

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

这将创建一个包含count = 0元素的数组。任何进一步的访问都将超出限制。

我建议您使用std::vector

std::vector<Kingdom> kingdoms(/*(good) count*/); 

以及std::string作为您的char m_name[32];类型.

当我看到这行时:

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

我希望像循环跟随这样的东西,而不是if.还有一些代码首先用一些名称填充数组。

if => for 版后:您应该打印

王国名不是维斯特洛的一部分。

循环后不在里面,只有当KingdomName没有找到时。

也许您只是忘记将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;
  }

同样在编辑后,也许源代码中的这样的东西.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;
        }
    }
}