c – 如果语句不起作用,则跳过其他部分

//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,人口234567
The_Vale不是维斯特洛的一部分
The_Vale不是维斯特洛的一部分
The_Vale不是维斯特洛的一部分

最佳答案 也许您只是忘了将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;
        }
    }
}
点赞