首先,我知道你会说这是一个已知且已经解决的问题,但我尝试了这样做,并得到一个输出,即常量字符串不是数组的一部分。
具体来说,我添加了一个名为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 :(得分:0)
您需要使用strcmp
之类的内容来比较2个字符串的内容;事实上,你正在比较他们在内存中的位置,(正如你所看到的)是不一样的。