-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
ファイルの関数を呼び出します。私kingdom.h
ファイル内
#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;
}
最後に、私の構造体は、含ま:
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);
ありがとう、strcmpについて忘れてしまった。 – elvisi27