【问题标题】:how to find a specific target in the array c++如何在数组c ++中找到特定目标
【发布时间】:2022-11-14 03:15:55
【问题描述】:

搜索功能应该从用户那里获取一个值来搜索它,如果找到了这个值,那么它应该打印出来,如果没有找到它应该打印 not found。 但是,在我的代码中,每次我写数组中的数字时,都会给我一个 false 选项,尽管它在存储的数组中。 `

#include <iostream>
using namespace std;
const int size = 100;
int partsmenu(int menu_option);
void readparts(char part_number[][10], double price[], char classification[], int& number_of_parts);
int search(char part_number[][10], char search_target[], int number_of_parts, double price[], char classification []);
void display_parts(char part_number[][10], double price[], char classification[], int& number_of_parts);

int main()
{
    const int size = 100;
    int menu_option=0, option, displaysearch;
    char part_number[size][10];
    double price[size];
    char classification[size];
    int number_of_parts = 0;
    char search_target[size];

    //using switch statment to make it look like a menu option
    do {
        switch (option = partsmenu(menu_option))
        {
        case 1:
            readparts(part_number, price, classification, number_of_parts);
            break;

        case 2:
            display_parts(part_number, price, classification, number_of_parts);
            break;

        case 3:
            displaysearch = search(part_number, search_target, number_of_parts, price, classification);
            break;

        case 4:
            break;

        default:
            cout << "Not valid..." << endl;
            break;
        }
        cout << endl;
    } while (option != 4);

    return 0;
}
int partsmenu(int menu_option)
{
    cout <<"1) Enter new part number\n2) View all part numbers\n3) Search for part\n4) Exit\n\nEnter an option: ";
    cin >> menu_option;

    return menu_option;
}

void readparts(char part_number[][10], double price[], char classification[], int& number_of_parts)
{
    // using for loop to store part number, price, and classification in the array
    int number;
    cout << "Enter number of parts to add:";
    cin >> number;
    cout << endl;
    int i;
    for (i = number_of_parts; i < (number_of_parts+number); i++)
    {
        cout << "Enter part number: ";
        cin >> part_number[i];
        cout << "Enter price: ";
        cin >> price[i];
        cout << "Enter classificarion: ";
        cin >> classification[i];

        //using if statment to check for the classificarion
        if (classification[i] == 'A' || classification[i] == 'B' || classification[i] == 'C')
            cout << "";
        else
        {
            cout << "Invalid case..." << endl;
            cout << "Enter Valid class [A, B, C]: ";
            cin >> classification[i];
            cout << endl;
        }

        cout << endl;
    }
    number_of_parts = i;

}

int search(char part_number[][10], char search_target[], int number_of_parts, double price[], char classification[])
{
    //searching for specific data
    bool found = false;
    int value;
    
    cout << "Enter part number: ";
    for (int j = 0; j < number_of_parts; j++)
    {
        cin >> search_target;
    

        for (int i = 0; i < number_of_parts && found == false; i++)
        {
            if (part_number[i] == search_target)
                found = true;
                value = i;

        }
    }

    if (found == true)
    {
        for (int i = 0; i < number_of_parts; i++)
        {
            cout << "Part ID\t\tPrice\t\tClass" << endl;
            cout << " --------------------------------------------" << endl;
            cout << part_number[value] << "\t\t" <<price[value]<< "\t\t" <<classification[value]<< endl;
        }
    }
    else
    {
        cout << "No parts found..." << endl;
        value = -1;
    }
    return value;
    
}

void display_parts(char part_number[][10], double price[], char classification[], int& number_of_parts)
{
    // displaying the data
    cout << "Part ID\t\tPrice\t\tClass" << endl;
    cout << "--------------------------------------------" << endl;
    for (int i = 0; i < number_of_parts; i++)
    {
        cout << part_number[i] << "\t\t" << price[i] << "\t\t" << classification[i] << endl;
    }
    cout << endl;
}


`

我试图找出代码有什么问题,但找不到任何错误。其他一切正常。

【问题讨论】:

  • 这个问题可以用一个简单的 5 行或 10 行程序来复制,没有菜单等。这就是你减少问题的方式——如果提供的答案是正确的,那么发布的大部分代码都是无关紧要的,并且问题归结为不知道如何比较两个字符数组。此外,调试代码时,您会看到您认为会比较为 true 的比较结果返回为假。所有这些信息都会对您有所帮助,也许足以研究为什么 == 不适用于 char 数组,或者至少提到您注意到了这个问题。
  • 切线:您会发现使用 structs 的数组而不是几个单独的数组更容易

标签: c++ arrays


【解决方案1】:

您将 C 字符串与 strcmp 而非 == 进行比较。像这样

if (strcmp(part_number[i], search_target) == 0)

如果您使用==,那么您所比较的只是字符串的地址,即使字符串内容相同,两个字符串的地址也可以不同。

strcmp 比较字符串中的实际字符,而不是地址。如果两个字符串相同,则返回0

【讨论】:

  • 哦,好吧,现在我明白为什么它不起作用了。我没有想到使用 strcmp
  • 现在我们有了第 20 亿个副本。恭喜@约翰
猜你喜欢
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多