If the instruction does not work and is skipped to another part

//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

Now he is typing

Mordor is not part of Westeros Mordor is not part of Westeros Mordor is not part of Westeros Mordor is not part of Westeros Mordor is not part of Westeros

The_Vale is not part of Westeros The_Vale, population 234567 The_Vale is not part of Westeros The_Vale is not part of Westeros The_Vale is not part of Westeros

+4
source share
2 answers

Perhaps you just forgot to add the array The_Valeto the array pKingdoms.

So, something like this will make you understand what you were doing wrong:

, , ;)

    //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;
        }
    }
}
0
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 edition:

KingdomName Westeros.

, KingdomName .

+1

Source: https://habr.com/ru/post/1668340/


All Articles