将对象成员数组与字符串进行比较
Comparing an object member array with a string
首先,我知道你会说这是一个已知且已解决的问题,但我尝试这样做,我得到的输出是常量字符串不是数组的一部分。
具体来说,我添加了一个名为 The_Vale
的数组元素,当我执行包含The_Vale
字符串的显示函数时,它说"The_Vale不是维斯特洛的一部分"。
代码对我来说看起来不错,我不知道问题出在哪里。你能帮我做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);
您需要使用 strcmp
之类的东西来比较 2 个字符串的内容; 事实上,您正在比较它们在内存中的位置,这些位置(如您所见)并不相同。
相关文章:
- 视觉C++使用 map 来比较字符串中的每个单词
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- C++使用关系运算符比较字符串
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- C++ TCP 服务器比较字符串失败
- 如何在不复制的情况下比较字符串的一部分?
- 比较 C++ 字符串打印中的两个整数数组
- 如何比较字符串形式的临时变量
- std::out_of_range 比较字符串中的符号时出错
- 比较字符串大小写不区分的简单方法是什么?
- 比较字符串忽略 NUL
- 比较字符串(arduino)时出现问题
- 如何在C++中比较字符串
- 比较字符串对象等于向量中的元素
- C 比较字符串操作速度
- 需要比较字符串的某些元素
- 比较字符串 .c_str() 和普通字符串的差异
- C 比较字符串的字符
- 通过使用两个不同的队列比较字符串