-1

The search function should get a value from the user to search for it, if the value is found, then it should print it out, and if not found it should print not found. However, in my code every time I write the number that is in the array is gives me the false option although it is in the array stored. `

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


`

I am trying to find what is wrong with code but could not find any fault with it. Everything else works fine.

  • The issue could have been duplicated with a simple 5 or 10 line program, without menus, etc. That is how you whittle the problem down -- if the answer provided is the right one, then most of the code posted was irrelevant, and the issue came down to not knowing how to compare two char arrays. Also, debugging the code, you would have seen that comparisons you thought would compare as `true` were coming back as false. All of this information would have helped you, maybe enough to research why `==` wasn't working for char arrays, or at the very least, mention that you noticed this issue. – PaulMcKenzie Nov 13 '22 at 18:58
  • 1
    Tangential: you would find it easier with an array of `struct`s, rather than several individual arrays – Paul Sanders Nov 13 '22 at 18:58
  • Tactical note: Write a bit of code, just enough to do one simple thing. Test that code until you know you can trust it, and only then write a little more code. Test it. Repeat. Sounds slow, but the writing of the code is usually insignificant compared to the time you'll spend debugging it, so if you spend time making the code less likely to contain bugs or make it easier to isolate bugs( if you added three lines of code and now have a bug, usually the bug's in or related to those three new lines) you save time over all. – user4581301 Nov 13 '22 at 19:29
  • You'll find that even the grand master programmers don't write a whole program in one shot unless it is trivial and short. They do the same thing, write a little-test a little, but they probably wrote a few more lines between tests than a rookie can get away with. – user4581301 Nov 13 '22 at 19:32
  • Does this answer your question? [Check if element found in array c++](https://stackoverflow.com/questions/19215027/check-if-element-found-in-array-c) – thoerni Nov 14 '22 at 22:17

1 Answers1

0

You compare C strings with strcmp not ==. Like this

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

If you use == then all you are comparing is the addresses of the strings, the addresses of two strings can be different even if the string contents are the same.

strcmp compares the actual characters in the strings, not the addresses. It returns 0 if the two strings are the same.

john
  • 85,011
  • 4
  • 57
  • 81