Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 搜索字符数组_C++ - Fatal编程技术网

C++ 搜索字符数组

C++ 搜索字符数组,c++,C++,在CPP文件#1中,我尝试在数组中循环,以查看输入的名称是否与Mordor或_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

在CPP文件#1中,我尝试在数组中循环,以查看输入的名称是否与Mordor或_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;
 }
#头文件#
#伊芬德夫王国
#定义王国
命名空间韦斯特罗斯
{
阶级王国
{
public://使该类向代码的其余部分公开
字符m_名称[32];
国际人口;
整数计数=0;
};
虚空显示(王国&);
无效显示(王国*k,整数x);
无效显示(王国*k,整数x,整数z);
无效显示(王国*k,整数x,字符foo[]);
}
#恩迪夫
#CPP文件#1#
#包括
#包括“kingdom.h”
无效显示(王国*k,整数x,字符foo[])
{
int a=0;
int=0;
库特
if(k[a].m_name==foo)

这不是比较两个C样式字符串的方式。这只是比较指针,几乎肯定会导致false。您可以使用
strcmp
#include
):

一个更好的方法,因为你在C++中编程,使用<代码> STD::String < /C> >:

std::string m_name;
而这种比较将完美无瑕地发挥作用

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

这不是比较两个C样式字符串的方式。这只是比较指针,几乎肯定会导致false。您可以使用
strcmp
#include
):

一个更好的方法,因为你在C++中编程,使用<代码> STD::String < /C> >:

std::string m_name;
而这种比较将完美无瑕地发挥作用