0
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"

Edited by lochnessmonster: n/a

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Caligulaminus
0

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;
}
1

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 ...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.