2017-01-30 5 views
0

В файле # 1 CPP я пытаюсь перебрать массив, чтобы увидеть, совпадает ли какое-либо из введенных имен с Mordor или the_Vale (из файла CPP # 2 в нижней части), однако мой цикл не является работать, и я только знаю, как перебрать массив строк, а не символПоиск по массиву символов

#Header File# 

#ifndef KINGDOM_H 
#define KINGDOM_H 
namespace westeros 
{ 
    class Kingdom 
    { 
    public: //Makes this class public to the rest of the code 

     char m_name[32]; 
     int m_population; 
     int count = 0; 
}; 
    void display(Kingdom&); 
    void display(Kingdom* k, int x); 
    void display(Kingdom* k, int x, int z); 
    void display(Kingdom* k, int x, char foo[]); 
    } 
    #endif 



#CPP FIle #1# 
#include <iostream> 
#include "kingdom.h" 


void display(Kingdom* k, int x, char foo[]) 
{ 
    int a = 0; 
    int found = 0; 

    cout << "Searching for kingdom " << foo << " in Westeros" << endl; 
    for (a; a < x; a++) 
    { 
     if (k[a].m_name == foo) 

//(strcmp(k[a].m_name) == 0)//Not working 
     { 
      cout << k[a].m_name << ", population " << k[a].m_population << endl; 
      found = 1; 
     } 
    } 
    if (found == 0) 
    { 
     cout << foo << " is not part of Westeros." << endl; 
    } 
} 

} 



## CPP File (main) #2## 
#include <iostream> 
#include "kingdom.h" 

using namespace std; 
using namespace westeros; 

int main(void) 
{ 
int count = 0; // the number of kingdoms in the array 

       // TODO: declare the kingdoms pointer here (don't forget to initialize it) 
Kingdom* pKingdoms = nullptr; 


cout << "==========" << endl 
    << "Input data" << endl 
    << "==========" << endl 
    << "Enter the number of kingdoms: "; 
cin >> count; 
cin.ignore(); 


pKingdoms = new Kingdom[count]; 


for (int i = 0; i < count; ++i) 
{ 
    // TODO: add code to accept user input for the kingdoms array 
    int x = 0; 
    x++; 
    cout << "Enter the name for kingdom #" << x + i << ": "; 
    cin >> pKingdoms[i].m_name; 
    cout << "Enter the number people living in " << pKingdoms[i].m_name << ": "; 
    cin >> pKingdoms[i].m_population; 

} 
cout << "==========" << endl << endl; 


// testing that "display(...)" works 
cout << "------------------------------" << endl 
    << "The first kingdom of Westeros" << endl 
    << "------------------------------" << endl; 
display(pKingdoms[0]); 
cout << "------------------------------" << endl << endl; 

// This is where I am having the problem 
display(pKingdoms, count, "Mordor"); 
cout << endl; 

display(pKingdoms, count, "The_Vale"); 
cout << endl; 

cout << endl; 

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

ответ

4

if (k[a].m_name == foo)

Это не так, как вы сравнить две строки C-Style. Это только сравнивает указатели, что должно привести к ложному почти наверняка. Вы можете использовать strcmp (#include <string.h>):

if (!strcmp(k[a].m_name, foo)) 

лучший способ, хотя, так как вы программируете в C++, используйте std::string:

std::string m_name; 

и сравнение работали бы безупречно.