简体   繁体   中英

Data structure that requires a search function in c++

this is our tasks and I don't know what command should I use to put a search function. it should use a search function to search for the Employee ID No. If found, display the details. Otherwise, tell the user that record not found. these are the requirements the user inputs 6 employee records with the corresponding data structure fields: a. ID No b. Name c. Age d. Gender (M/F) e. Address f. Salary

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

struct Info {
    string Id;
    string Name;
    string Age;
    string Gender;
    string Address;
    string Salary;

}i1, i2, i3, i4, i5, i6;

void printdata(Info data);
//storing data
int main() {

    string staff1;
    cout << "ID Number: ";
    getline(cin, staff1);
    cout << "Name: ";
    getline(cin, i1.Name);
    cout << "Age: ";
    getline(cin, i1.Age);
    cout << "Gender(M/F): ";
    getline(cin, i1.Gender);
    cout << "Address: ";
    getline(cin, i1.Address);
    cout << "Salary: ";
    getline(cin, i1.Salary);
    cout << "\n";

    string staff2;
    cout << "ID Number: ";
    getline(cin, staff2);
    cout << "Name: ";
    getline(cin, i2.Name);
    cout << "Age: ";
    getline(cin, i2.Age);
    cout << "Gender(M/F): ";
    getline(cin, i2.Gender);
    cout << "Address: ";
    getline(cin, i2.Address);
    cout << "Salary: ";
    getline(cin, i2.Salary);
    cout << "\n";

    string staff3;
    cout << "ID Number: ";
    getline(cin, staff3);
    cout << "Name: ";
    getline(cin, i3.Name);
    cout << "Age: ";
    getline(cin, i3.Age);
    cout << "Gender(M/F): ";
    getline(cin, i3.Gender);
    cout << "Address: ";
    getline(cin, i3.Address);
    cout << "Salary: ";
    getline(cin, i3.Salary);
    cout << "\n";

    string staff4;
    cout << "ID Number: ";
    getline(cin, staff4);
    cout << "Name: ";
    getline(cin, i4.Name);
    cout << "Age: ";
    getline(cin, i4.Age);
    cout << "Gender(M/F): ";
    getline(cin, i4.Gender);
    cout << "Address: ";
    getline(cin, i4.Address);
    cout << "Salary: ";
    getline(cin, i4.Salary);
    cout << "\n";

    string staff5;
    cout << "ID Number: ";
    getline(cin, staff5);
    cout << "Name: ";
    getline(cin, i5.Name);
    cout << "Age: ";
    getline(cin, i5.Age);
    cout << "Gender(M/F): ";
    getline(cin, i5.Gender);
    cout << "Address: ";
    getline(cin, i5.Address);
    cout << "Salary: ";
    getline(cin, i5.Salary);
    cout << "\n";

    string staff6;
    cout << "ID Number: ";
    getline(cin, staff6);
    cout << "Name: ";
    getline(cin, i6.Name);
    cout << "Age: ";
    getline(cin, i6.Age);
    cout << "Gender(M/F): ";
    getline(cin, i6.Gender);
    cout << "Address: ";
    getline(cin, i6.Address);
    cout << "Salary: ";
    getline(cin, i6.Salary);
    cout << "\n";

    string a;
    cout << "\nEnter Employees ID Number: ";
    getline(cin, a);

    if (a == staff1) {
        printdata(i1);
    }
    else if (a == staff2) {
        printdata(i2);
    }
    else if (a == staff3) {
        printdata(i3);
    }
    else if (a == staff4) {
        printdata(i4);
    }
    else if (a == staff5) {
        printdata(i5);
    }
    else if (a == staff6) {
        printdata(i6);
    }
    return 0;
    }
//layout of data output
void printdata(Info data)
{
    cout << "\n\n";
    cout << "Name: " << data.Name << endl;
    cout << "Age: " << data.Age << endl;
    cout << "Gender: " << data.Gender << endl;
    cout << "Address: " << data.Address << endl;
    cout << "Salary: " << data.Salary << endl;
}

You mention the term function. The task is to write a search function.

And you know functions, because you have written one: printdata .

What is not a good implementation, is to create 6 different sets of variables and then reading them step by step. This is not good.

Also here you must use a function. What would you have done, if the teacher would have said: Use 100 Info data?

So, first step is to write a function for the input. For you important to know is, how to return values from a function. There are basically 3 possibilites.

  1. Pass an Info ass pointer
  2. Pass an info as reference
  3. Return the Info from the function

I will select number 3 to give you an example.

Info getInfo() {

    Info result;

    cout << "ID Number: ";
    getline(cin, result.Id);
    cout << "Name: ";
    getline(cin, result.Name);
    cout << "Age: ";
    getline(cin, result.Age);
    cout << "Gender(M/F): ";
    getline(cin, result.Gender);
    cout << "Address: ";
    getline(cin, result.Address);
    cout << "Salary: ";
    getline(cin, result.Salary);
    cout << "\n";

    return result;
}

With that your program can already be shortened very much and look better. We eliminated much code repetition by using a function.

The next version of your code would then look like this:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

struct Info {
    string Id;
    string Name;
    string Age;
    string Gender;
    string Address;
    string Salary;

}i1, i2, i3, i4, i5, i6;

Info getInfo() {

    Info result;

    cout << "ID Number: ";
    getline(cin, result.Id);
    cout << "Name: ";
    getline(cin, result.Name);
    cout << "Age: ";
    getline(cin, result.Age);
    cout << "Gender(M/F): ";
    getline(cin, result.Gender);
    cout << "Address: ";
    getline(cin, result.Address);
    cout << "Salary: ";
    getline(cin, result.Salary);
    cout << "\n";

    return result;
}


void printdata(Info data);
//storing data
int main() {

    i1 = getInfo();
    i2 = getInfo();
    i3 = getInfo();
    i4 = getInfo();
    i5 = getInfo();
    i6 = getInfo();

    string a;
    cout << "\nEnter Employees ID Number: ";
    getline(cin, a);

    if (a == i1.Id) {
        printdata(i1);
    }
    else if (a == i2.Id) {
        printdata(i2);
    }
    else if (a == i3.Id) {
        printdata(i3);
    }
    else if (a == i4.Id) {
        printdata(i4);
    }
    else if (a == i5.Id) {
        printdata(i5);
    }
    else if (a == i6.Id) {
        printdata(i6);
    }
    return 0;
    }
//layout of data output
void printdata(Info data)
{
    cout << "\n\n";
    cout << "Name: " << data.Name << endl;
    cout << "Age: " << data.Age << endl;
    cout << "Gender: " << data.Gender << endl;
    cout << "Address: " << data.Address << endl;
    cout << "Salary: " << data.Salary << endl;
}

Bu this is of course still not good. Therefore the next important information is that we can use so a called "arrays". In arrays you can store 1 or many instances of the same data type.

And in your example you have to store 6 instances of the same type "Info".

How can this be done. In C++ we would always uses a std::vector or a std::array . You may read here and here about those.

But most probably you did not learn about them yet. But, no worries, C++ understands some legacy code from C and so we can use C-Style arrays. Please read here about them. I guess, you saw it already.

Now, if you want to have a data structure for 6 Info's. You can write:

Info data[6];

And then you can store 6 instances of info in data. Using the index operator [] .

And to avoid redundant writing we can use for loop. Please read here about for loops.

Additionally we will add a function for searching.

This will lead us to the nearly final implementation:

#include<iostream>
#include<string>
#include<sstream>
using namespace std;

struct Info {
    string Id;
    string Name;
    string Age;
    string Gender;
    string Address;
    string Salary;

};

Info getInfo() {

    Info result;

    cout << "ID Number: ";
    getline(cin, result.Id);
    cout << "Name: ";
    getline(cin, result.Name);
    cout << "Age: ";
    getline(cin, result.Age);
    cout << "Gender(M/F): ";
    getline(cin, result.Gender);
    cout << "Address: ";
    getline(cin, result.Address);
    cout << "Salary: ";
    getline(cin, result.Salary);
    cout << "\n";

    return result;
}

void printdata(Info data);

void searchAndPrint(Info data[6]) {
    string a;
    cout << "\nEnter Employees ID Number: ";
    getline(cin, a);

    for (int i = 0; i < 6; ++i) {
        if (a == data[i].Id)
            printdata(data[i]);
    }
}

//storing data
int main() {

    Info data[6];

    for (int i = 0; i < 6; ++i) {
        data[i] = getInfo();
    }
    searchAndPrint(data);
  
    return 0;
}
//layout of data output
void printdata(Info data)
{
    cout << "\n\n";
    cout << "Name: " << data.Name << endl;
    cout << "Age: " << data.Age << endl;
    cout << "Gender: " << data.Gender << endl;
    cout << "Address: " << data.Address << endl;
    cout << "Salary: " << data.Salary << endl;
}

And alittle bit more advanced, using more C++ elements:

#include<iostream>
#include<string>

struct Info;                          // Forward declaration
constexpr size_t NumberOfInfo = 6u;   // We want to use 6 Info structs
using InfoData = Info[NumberOfInfo];  // Define the type of the array

struct Info {                         // Structure to store info abaout a person
    std::string Id;
    std::string Name;
    std::string Age;
    std::string Gender;
    std::string Address;
    std::string Salary;
};

// Get info data from user
void getInfo(InfoData &data) {

    for (size_t i = 0u; i < NumberOfInfo; ++i) {

        std::cout << "ID Number: ";
        std::getline(std::cin, data[i].Id);
        std::cout << "Name: ";
        std::getline(std::cin, data[i].Name);
        std::cout << "Age: ";
        std::getline(std::cin, data[i].Age);
        std::cout << "Gender(M/F): ";
        std::getline(std::cin, data[i].Gender);
        std::cout << "Address: ";
        std::getline(std::cin, data[i].Address);
        std::cout << "Salary: ";
        std::getline(std::cin, data[i].Salary);
        std::cout << "\n";
    }
}

// Print one Info data to std::cout
void printdata(Info &data)
{
    std::cout << "\n\nName: " << data.Name << '\n'
        << "Age: " << data.Age << '\n'
        << "Gender: " << data.Gender << '\n'
        << "Address: " << data.Address << '\n'
        << "Salary: " << data.Salary << '\n';
}

// Search and ID and, if found, show the Info
void searchAndPrint(InfoData &data) {

    std::string a;
    std::cout << "\nEnter Employees ID Number: ";
    std::getline(std::cin, a);

    for (int i = 0; i < NumberOfInfo; ++i) {
        if (a == data[i].Id)
            printdata(data[i]);
    }
}

// Main function
int main() {

    InfoData data;

    getInfo(data);
    searchAndPrint(data);

    return 0;
}

The intersting point in the last solution is:

If you really want to use full C++ and with that std::array , then the only difference is

  1. to inlcude <array> and
  2. to replace the line using InfoData = Info[NumberOfInfo]; with using InfoData = std::array<Info,NumberOfInfo>;

This should work:

#include<iostream>
#include<string>

const int arr_size = 6; // No. of employees you want to store

struct Info {
    std::string Id;
    std::string Name;
    std::string Age;
    std::string Gender;
    std::string Address;
    std::string Salary;

}info[arr_size]{};

void search(int id);

//storing data
int main() {

    for (int i = 0; i < arr_size; i++)
    {
        std::cout << "ID Number: ";
        std::getline(std::cin, info[i].Id);

        std::cout << "Name: ";
        std::getline(std::cin, info[i].Name);

        std::cout << "Age: ";
        std::getline(std::cin, info[i].Age);

        std::cout << "Gender(M/F): ";
        std::getline(std::cin, info[i].Gender);

        std::cout << "Address: ";
        std::getline(std::cin, info[i].Address);

        std::cout << "Salary: ";
        std::getline(std::cin, info[i].Salary);

        std::cout << "\n";
    }

    std::string a;
    std::cout << "\nEnter Employees ID Number: ";
    std::getline(std::cin, a);

    search(atoi(a.c_str()));

    return 0;
}

//layout of data output
void search(int id)
{
    for (int i = 0; i < arr_size; i++)
    {
        if (std::to_string(id) == info[i].Id)
        {
            std::cout << "\n\n";
            std::cout << "Name: " << info[i].Name << std::endl;
            std::cout << "Age: " << info[i].Age << std::endl;
            std::cout << "Gender: " << info[i].Gender << std::endl;
            std::cout << "Address: " << info[i].Address << std::endl;
            std::cout << "Salary: " << info[i].Salary << std::endl;
        }
    }
}

To improve this, you can also use a vector, which is essentially a dynamic list type:

#include <iostream>
#include <string>
#include <vector>
#include <limits>

#undef max

struct Info {
    std::string Id;
    std::string Name;
    std::string Age;
    std::string Gender;
    std::string Address;
    std::string Salary;

}; std::vector<Info> info{};

void search(int id);

//storing data
int main() {

    while (true)
    {
        info.push_back(Info());

        while (true)
        {
            std::cout << "ID Number: ";
            std::getline(std::cin, info[info.size() - 1].Id);

            for (int i = 0; i < info.size() - 1; i++)
            {
                if (info[info.size() - 1].Id == info[i].Id)
                {
                    std::cout << "Duplicate id found!" << std::endl;
                    goto cont;
                }
            }
            break;
            cont: continue;
        }

        std::cout << "Name: ";
        std::getline(std::cin, info[info.size() - 1].Name);

        std::cout << "Age: ";
        std::getline(std::cin, info[info.size() - 1].Age);

        std::cout << "Gender(M/F): ";
        std::getline(std::cin, info[info.size() - 1].Gender);

        std::cout << "Address: ";
        std::getline(std::cin, info[info.size() - 1].Address);

        std::cout << "Salary: ";
        std::getline(std::cin, info[info.size() - 1].Salary);

        std::cout << "\n";

        std::cout << "Do you want to enter more employees? (Y - Yes / N - No)";
        std::string choice; std::getline(std::cin, choice);

        if (std::tolower(choice[0]) != 'y') break;
    }

    std::string a;
    std::cout << "\nEnter Employees ID Number: ";

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::getline(std::cin, a);

    std::cout << "\n\n";
    search(atoi(a.c_str()));

    return 0;
}

//layout of data output
void search(int id)
{
    for (int i = 0; i < info.size(); i++)
    {
        if (std::to_string(id) == info[i].Id)
        {
            std::cout << "Name: " << info[i].Name << std::endl;
            std::cout << "Age: " << info[i].Age << std::endl;
            std::cout << "Gender: " << info[i].Gender << std::endl;
            std::cout << "Address: " << info[i].Address << std::endl;
            std::cout << "Salary: " << info[i].Salary << std::endl;

            return;
        }
    }

    std::cout << "No employee with id: " << id << " found" << std::endl;
}

For the std::vector option, I've also added the ability to enter a variable amount of employees, which can be desirable by the user almost all the time. I have also removed the possibility for duplicate id.

If you're using the std::vector option, do make sure to compile with c++11, for eg:

g++

$ g++ -std=c++11 your_file.cpp

Also, you should not use the following line (I've removed it):

using namespace std;

...as it is considered as bad practice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM