lundi 30 janvier 2017

if statement is not working and is skipped to else 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;
        if (pKingdom[kingdomElement].m_name == KingdomName){
            cout << " --------------------- " << endl;
            cout << KingdomName << ", population " << pKingdom[kingdomElement].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

How it should work is that when I put in The_Vale in my array, it should say The_Vale, population, population size of kingdom in integer for my display function and

When I call for Mordor's case, since Mordor is not part of Westeros, it should print out message saying Mordor is not part of Westeros since I didn't put Mordor in my array.

But, the problem is that the code prints out message that THe_Vale is not part of Westeros when it should say The_Vale, population, and population size of vale since it is in the array. Does anybody know how to solve this problem?

Aucun commentaire:

Enregistrer un commentaire