0
struct app
{   int id;
    char name[20];
    char developer[20];
    char type[20];
    int date_uploaded [3];
};
.
.
.
void search(app a[2])
{
    int choice;
    char t_check[20];
    Dhome();
    cout<<"\n\nSelect the way you want to search for an app"
        <<"\n(1) To search by app name"
        <<"\n(2) To search by developer name"
        <<"\n(3) To search by type"
        <<"\n(4) To return to previous menu\n";
    cin>>choice;
    cin.ignore();
    switch(choice)
    {
        case 1:
            Dhome();
            cout<<"\n\nEnter the app's name: ";
            search_specific(a,"name");
            //Similar cases for passing developer and type
    }
}
void search_specific(app a[2], char choice[10])
    {   int i, flag=0;
        char t_check[20], s_type[5];
        gets(t_check);
        for(i=0; i<2; i++)
        {
        if(strcmp(t_check, a[i].choice)==0)
            {
                flag=1;
                break;
            }
        }
        if(flag==1)
        {
            cout<<"\nThe app was found and its details are as below"
                <<"\nApp ID: "<<a[i].id
                <<"\nApp Name: "<<a[i].name
                <<"\nDeveloper Name: "<<a[i].developer
                <<"\nType: "<<a[i].type
                <<"\nDate Uploaded: "<<a[i].date_uploaded[0]<<"/"<<a[i].date_uploaded[1]<<"/<<a[i].date_uploaded[2];
            cout<<"\n\nPress enter to return to the previous menu";
            getch();
            return;
        }
        if(flag==0)
        {
            cout<<"\nThe app was not found";
            cout<<"\n\nPress enter to return to the previous menu";
            getch();
            return;
        }

    }

Now the problem is that I can't use a[i].choice because the compiler tries to find 'choice' in the structure app. I want it to refer to its value i.e name, developer or type. How can I accomplish this?

2
  • 1
    Please read: stackoverflow.com/help/mcve Commented Dec 16, 2014 at 7:37
  • 1
    app doesn't have a member named choice. Commented Dec 16, 2014 at 7:37

2 Answers 2

1

You would have to formulate a switch statement, reading the desired member of a value of type app like the following.

int choice;
// read choice from somewhere
switch(choice)
{
    case 0:
        // do something with a[i].id
        break;
    case 1:
        // do something with a[i].name
        break;
    default;
        break;
}

The members of a struct cannot be accessed index-wise.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but if I use switch then i will have to reproduce the search_specific code in each case.
0

The C++ language doesn't offer first-class properties that you can reflect over at run time, like JavaScript does. So there's no one-line answer to your question. Maybe the closest thing to what you're looking for is a std::map. But if you're working with a struct like app, you'll have to write your own code to fetch each field from an object, as in Codor's example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.