2017-01-29 3 views
-1

Прежде всего, я знаю, что вы сказали бы, что это известный и решаемый вопрос, но я попытался сделать это, и я получаю вывод, что константная строка не является частью массива.Сравнение массива объектов с строкой

В частности, я добавляю элемент массива с именем The_Vale, и когда я выполняю функцию отображения, содержащую строку The_Vale, в ней говорится: «The_Vale не является частью Westeros».

Код выглядит хорошо для меня, я не знаю, где проблема. Можете ли вы мне помочь с функцией void display(Kingdom* a, int index, const char *n)? Другие перегруженные функции работают нормально.

#include <iostream> 

#include "kingdom.h" 

namespace westeros { 

// TODO:definition for display(...) 

void display(Kingdom& p) { 
    cout << p.m_name << ", population " << p.m_population << endl; 
} 
void display(Kingdom* a, int index) { 

    int i; 
    int total_pop = 0; 
    cout << "------------------------------" << endl; 
    cout << " Kingdoms of Westeros" << endl; 
    cout << "------------------------------" << endl; 
    for (i = 0; i < index; i++) { 
     total_pop += a[i].m_population; 
     cout << i + 1 << "." << a[i].m_name << ", population " << a[i].m_population << endl; 
    } 
    cout << "------------------------------" << endl; 
    cout << "Total population of Westeros :" << total_pop << endl; 
    cout << "------------------------------" << endl; 
} 

void display(Kingdom* a, int index, int min) { 
    int i; 
    cout << "------------------------------" << endl; 
    cout << "Kingdoms of Westeros with more than" << min << " people" << endl; 
    cout << "------------------------------" << endl; 
    for (i = 0; i < index; i++) { 
     if (a[i].m_population >= min) { 
      cout << a[i].m_name << ", population " << a[i].m_population << endl; 
     } 
    } 
    cout << "------------------------------" << endl; 
} 
***void display(Kingdom* a, int index, const char *n) { 
    int i; 
    int found; 
    cout << "------------------------------" << endl; 
    cout << "Searching for kingdom " << n << " in Westeros" << endl; 
    cout << "------------------------------" << endl; 
    for (i = 0; i < index; i++) { 
     cout << a[i].m_name << " is being compared to " << n << endl; 
     if (a[i].m_name == n) { 
      found = 1; 
      cout << a[i].m_name << ", population " << a[i].m_population << endl; 
     } 
     else { 
      found = 0; 
     } 
    } 
    if (found == 1) { 
     cout << a[i].m_name << ", population " << a[i].m_population << endl; 
    } 
    if (found == 0) { 
     cout << n << " is not part of Westeros." << endl; 
    } 
    cout << "------------------------------" << endl; 
}*** 
} 

я вызываю функцию в моем главном .cpp файла.

#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 pKingdoms pointer here (don't forget to initialize it) 
    Kingdom* pKingdoms = NULL; 

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

    // TODO: allocate dynamic memory here for the pKingdoms pointer 
    pKingdoms = new Kingdom[count]; 

    for (int i = 0; i < count; ++i) 
    { 
     // TODO: add code to accept user input for the pKingdoms array 
     cout << "Enter the name for kingdom #" << i + 1 << ": "; 
     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; 


    // testing that the first overload of "display(...)" works 
    display(pKingdoms, count); 
    cout << endl; 


    // testing that the second overload of "display(...)" works 
    display(pKingdoms, count, 345678); 
    cout << endl; 


    // testing that the third overload of "display(...)" works 
    display(pKingdoms, count, "Mordor"); 
    cout << endl; 

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

    // TODO: deallocate the dynamic memory here 
    delete[] pKingdoms; 

    return 0; 
} 

Наконец, мой структуры в моем kingdom.h файл включен:

namespace westeros { 
struct Kingdom { 
    char m_name[9]; 
    int m_population; 
}; 
void display(Kingdom& p); 
void display(Kingdom* a, int index); 
void display(Kingdom* a, int index, int min); 
void display(Kingdom* a, int index, const char *n); 

ответ

0

Вы должны использовать что-то вроде strcmp сравнить содержание 2 строк; как это, вы сравниваете свои местоположения в памяти, которые (как вы видели) не совпадают.

+0

спасибо, забыли про strcmp. – elvisi27

Смежные вопросы