You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++数组搜索功能异常:输入存在的零件编号却返回未找到

零件搜索功能故障排查与修复

问题背景

需要实现零件编号搜索功能:从用户处获取待搜索的零件编号,找到则输出对应零件信息,未找到则提示未找到。但现有C++代码中,输入数组中已存储的零件编号时,始终返回“未找到”,其余功能正常。

问题代码

#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;
}

错误原因

  1. C风格字符串比较错误:直接用==比较char数组,实际比较的是数组的内存地址而非字符串内容,必须使用strcmp()函数判断字符串是否相等(返回0表示相等)。
  2. 冗余的循环逻辑:搜索函数中的外层for (int j = 0; j < number_of_parts; j++)完全多余,会强制用户输入number_of_parts次搜索目标,不符合“输入一次搜索编号”的需求。
  3. 代码块未加括号:内层循环中value = i没有被{}包裹,无论是否找到匹配项都会执行,导致value被覆盖为最后一个数组索引。
  4. 重复输出结果:找到匹配项后的for循环会重复打印结果number_of_parts次,只需打印一次即可。

修正后的代码

#include <iostream>
#include <cstring>  // 引入strcmp所需的头文件
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];

    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)
{
    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 classification: ";  // 修正拼写错误
        cin >> classification[i];

        if (!(classification[i] == 'A' || classification[i] == 'B' || classification[i] == 'C'))
        {
            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[])
{
    bool found = false;
    int value = -1;  // 初始化默认值
    
    cout << "Enter part number: ";
    cin >> search_target;  // 只需要输入一次搜索目标

    for (int i = 0; i < number_of_parts && !found; i++)
    {
        // 使用strcmp比较C风格字符串,返回0表示相等
        if (strcmp(part_number[i], search_target) == 0)
        {
            found = true;
            value = i;
        }
    }

    if (found)
    {
        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;
    }
    return value;
    
}

void display_parts(char part_number[][10], double price[], char classification[], int& number_of_parts)
{
    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;
}

额外优化点

  • 引入了<cstring>头文件,用于strcmp()函数。
  • 修正了readparts函数中“classificarion”的拼写错误。
  • 初始化value为-1,避免未赋值的变量问题。
  • 简化了分类校验的条件判断,逻辑更清晰。

内容的提问来源于stack exchange,提问作者Audreiallc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 10:46:16