C++ Reading variable names and values from a file

Hello

So I have a text file that looks some thing like this
one = 1
two = 2
three = 3.3
name = "Name"

How do I read this into my code so that the one, two, three etc become the variable names(in fact they already exist in the class) with their associated values from the file?
I can use std::map, but that's only good for a single variable type, like for example int.

Any thoughts?
> I can use std::map, but that's only good for a single variable type, like for example int.

Use std::map where the mapped_type is std::variant
https://en.cppreference.com/w/cpp/utility/variant
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Thing
{
   int one = 0, two = 0;
   double three = 0.0;
   string name;
public:
   istream &readline( istream &in )
   {
      string variable, value;
      char c;
      if ( in >> variable >> c >> value )
      {
         if      ( variable == "one"   ) one   = stoi( value );
         else if ( variable == "two"   ) two   = stoi( value );
         else if ( variable == "three" ) three = stod( value );
         else if ( variable == "name"  ) name  =       value  ;
         else                            cout << "?????\n";
      }
      return in;
   }
   void print() { cout << "one: " << one << "    two: " << two << "    three: " << three << "    name: " << name << '\n'; }
};



int main()
{
   Thing thing;
   istringstream in( "one = 1\n"
                     "two = 2\n"
                     "three = 3.3\n"
                     "name = Name\n" );
   while ( thing.readline( in ) );
   thing.print();
}
Cool thanks guys, l'll make one of these solutions work
Topic archived. No new replies allowed.