Data search [duplicate]

0

It is necessary to search for data by entering the type of aircraft.

This is what I have, but the result always shows that the registration does not exist.

void Search(AEROFLOT* array)
{
    char plane[20];
    cout << "\nEnter type of airplane: "; 
    fflush(stdin);
    gets(plane);
    for (int i = 0; i < n; i++)
    {
        if (plane == array[i].plane)
        {
            cout << "\nDestination: " << pm[i].name << endl;
            cout << "Number of airplane: " << pm[i].number << endl;
            cout << "\n" << endl;
            break;
        }

        else
        {
            cout << "\nThere isn't an airplane of this type.\n" << endl;
            break;
        }
    }
}

I know that there are sequential and binary search methods, but I would like to know how I can do a single search directly, since in the program all the data are first entered, then they are sorted in ascending order according to the airplane number and by Last search is done.

    
asked by Neon 15.11.2016 в 12:59
source

1 answer

0

To compare strings in C ++ you can not use operators like ==, & lt ;, > or other similar ones. C ++ is based on C, in which comparison operators can only be used to compare basic types (such as char, int, float, pointers, and the like). Other languages such as Java, C # and others with similar syntax correct these deficiencies, being able to make comparisons of complex types with these operators.

To make the comparison that you want to do you have to use some function like strcmp and other similar ones. For example, the comparison you make would look like this with strcmp:

if (strcmp (plane, array[i].plane) == 0)
    {
        cout << "\nDestination: " << pm[i].name << endl;
        cout << "Number of airplane: " << pm[i].number << endl;
        cout << "\n" << endl;
        break;
    }

Another option would be to use some type String type that overloads the operator == in order to make the code more intuitive.

In any case I recommend you check the code, because currently for each plane you want to search (and that exists in the array) will print a lot of texts "There is not an airplane of this type."

    
answered by 15.11.2016 в 13:28