Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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++,我正在浏览这个程序,我想知道如何在数组中搜索字母而不是数字,它对数字有效,但我怎样才能使它对字母有效。请帮忙。。。。。。。。。。。。。。。这是密码 #include <iostream> using namespace std; const int DECLARED_SIZE = 4; void fillArray(int a[], int size, string& letter); int search(const int a[], string letter, str

我正在浏览这个程序,我想知道如何在数组中搜索字母而不是数字,它对数字有效,但我怎样才能使它对字母有效。请帮忙。。。。。。。。。。。。。。。这是密码

#include <iostream>
using namespace std;
const int DECLARED_SIZE = 4;

void fillArray(int a[], int size, string& letter);
int search(const int a[], string letter, string target);

int main( )
{
    int arr[DECLARED_SIZE]; string listletter; string target;
    fillArray(arr, DECLARED_SIZE, listletter);
    char ans;
    int result;
    do
    {
        cout << "Enter a letter to search for: ";
        cin >> target;
        result = search(arr, listletter, target);
        if (result == -1)
            cout << target << " is not on the list.\n";
        else
            cout << target << " is stored in array position "
            << result << endl
            << "(Remember: The first position is 0.)\n";
        cout << "Search again?(y/n followed by Return): ";
        cin >> ans;
    } while ((ans != 'n') && (ans != 'N'));
    cout << "End of program.\n";
    return 0;
}

void fillArray(int a[], int size, string& letter)
{
    cout << "Enter up to " << size << " letter.\n"
        << "Mark the end of the list with a negative number.\n";
    int next, index = 0;
    cin >> next;
    while ((next >= 0) && (index < size))
    {
        a[index] = next;
        index++;
        cin >> next;
    }
}

int search(const int a[], string numberUsed, string target)
{
    int index  = 0; 
    string run = "run";
    bool found = false;

    while ((!found)) // && (index < numberUsed))
        if (target == run)
            found = true;
        else
            index++;

    if (found)
        return index;
    else
        return -1;
}
#包括
使用名称空间std;
const int声明的_SIZE=4;
void fillArray(int a[],int size,字符串和字母);
int搜索(常量int a[],字符串字母,字符串目标);
int main()
{
int arr[DECLARED_SIZE];字符串listletter;字符串目标;
fillArray(arr、声明的大小、listletter);
查尔安斯;
int结果;
做
{
cout>目标;
结果=搜索(arr、列表字母、目标);
如果(结果==-1)

cout我注意到你在fillArray中加载了一个int。如果你想输入字符,你需要使用char

void fillArray(int a[], int size, string& letter)
{
 cout << "Enter up to " << size << " letter.\n"
 << "Mark the end of the list with a negative number.\n";
 char next;
 int index = 0;
 cin >> next;
 while ((next >= 0) && (index < size))
 {
   a[index] = next;
   index++;
 cin >> next;
 }
}
void fillArray(int a[],int size,字符串和字母)
{
下一步;
}
}

你需要帮助的到底是什么,你收到了什么错误信息?Yikes.眼癌警报!尝试使用空格、缩进、格式和基本的美感!是家庭作业吗?正如Anders K.所说,你做了哪些尝试,哪些不起作用?人们回答问题,而不是为你编写代码。你可能想使用
字符s,而不是整个
string
int
。您已将局部变量更改为
char
,但未更改传递给函数的数组类型。还有一个未使用的参数,
string&letter
,但它存在于原始代码中。我试图最小化e需要对原始代码进行更改,但您有一个很好的观点。即使char可以存储为int,也应该使用char数组来明确您正在使用的内容。