std::string function(std::string buffer)
{
int Variable1 = 23;
if( !buffer.compare("Variable1"))   <--- i want the user to input the variable name..and have my program match the input with the variable name of the program and return the value it contains
     return Variable1; [U]<--- How do i convert this number into a c++ std::string?[/U]
}
return "turd"

Recommended Answers

All 3 Replies

You can use a stringstream, bjarne stroustrup has an example on his website under the FAQ section last I checked.


Try this on for size, and it can certainly be improved with error checking, etc.

#include <sstream>
#include <string>

template<typename value_type>
value_type FromString( std::string input ){
value_type r = 0;
std::stringstream(input) >> r;
return r;
}

How about a map?

std::map<std::string, int*>

Like

int func()
{
    int var1 = 2, var2 = 11, var3 = 5, var4 = 13;
    std::map<std::string, int*> dict;

    dict["var1"] = &var1;
    dict["var2"] = &var2;
    dict["var3"] = &var3;
    dict["var4"] = &var4;

    std::string s = "var3";

    return *dict[s];
}

Just an idea. No error checking, not tested, no ...

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.