How do you take input from a user and use that input to determine what information in a class to use. Here's an example that doesn't work, but hopefully you understand what I am trying to do from this.

class hello {
   string name;
   int size;
   };
int main() {
   string classname;
   hello world;
   world.name = "Earth";
   world.size = 100;

   hello people;
   people.name = "Everyone";
   people.size = 2;

   cin >> classname;
   cout << classname.name;
   return 0;

I want to take the user's input, classname, and use it to determine which instance of hello to use. The only way I was able to do this before was to use an if statement like:

if (classname == "world") {
   cout << world.name;
}
if (classname == "people") {
   cout << people.name;
}

but I was working on a project where I had over a hundred different options and that is a lot of if statements to write so I tried doing it differently. After hours upon hours of looking, I still have had no luck finding a way to do this so any info would be greatly appreciated.

Recommended Answers

All 2 Replies

It's not possible to 'convert' a string to a class with the same name as the value of the string.

What you can do is:
- Use a switch case, it is much more friendly as 100 if statements.
- Create a hash table where you can look up the string, and 'get' a pointer to the correct class back.

Probably the easiest way is to use a map (An associative container), which lets you associate "keys" (identifiers) with values/objects.

hello world;
    world.name = "Earth";
    world.size = 100;

    hello people;
    people.name = "Everyone";
    people.size = 2;

    std::map<std::string, hello> table;
    table["world"] = world;
    table["people"] = people;

STL maps come with a premade 'find' function, to let you find whether or not a 'key' exists within that map

std::map<std::string, hello>::iterator iter;
    iter = table.find(classname);

if the iterator returned by find reaches the end of the table, then that object doesn't exist in the map (strings are case sensitive too), otherwise you can use the iterator to access the object

if( table.end() == iter )
        std::cout << "object \"" << classname << "\" not found";
    else
    {
        hello& object = iter->second;
        std::cout << object.name << ' '
                  << object.size << std::endl;
    }

(quick example)

#include <string>
#include <iostream>
#include <map>

struct hello
{
   std::string name;
   int size;
};

void display_hello(const std::string& classname, std::map<std::string, hello>& table)
{
    std::map<std::string, hello>::iterator iter;
    iter = table.find(classname);

    if( table.end() == iter )
        std::cout << "object \"" << classname << "\" not found";
    else
    {
        hello& object = iter->second;
        std::cout << object.name << ' '
                  << object.size << std::endl;
    }
}

int main()
{
    hello world;
    world.name = "Earth";
    world.size = 100;

    hello people;
    people.name = "Everyone";
    people.size = 2;

    std::map<std::string, hello> table;
    table["world"] = world;
    table["people"] = people;

    display_hello("world", table);
    display_hello("people", table);
    display_hello("blah", table);
}

It would probably be better to wrap the map inside a handling class which looks after the map.

commented: Nice post +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.