Store a int with a name specified while running the app. ...

Lets say I have:
1
2
3
string i;
cout << "str: " << endl;
cin >> i;


Now I want to create a new int, inside the application, with the name of the value of the
string "i".

Say I made "i" the value of "trol", then how can I make a new string with the name "trol"?
Simply, you can't. How would you reference such a variable, anyway, which knowing what the user will input?
You don't know what the user will input, so you can't do such a thing. Why would you do it anyway ?
closed account (3hM2Nwbp)
That's a good question with plenty of practical uses.

You can simulate that by using a map.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <map>
#include <string>

int main()
{
  std::map<std::string, int> cache;
  std::cout << "Enter Cache Data Alias: " << std::endl;
  std::string alias;
  std::cin >> alias;
  std::cout << "Enter Cache Data Value: " << std::endl;
  int value = -1;
  std::cin >> value;
  cache.insert(std::make_pair(alias, value));

  std::cout << "Attributes:\n";
  for(std::map<std::string, int>::const_iterator iter = attributes.cbegin(); iter != attributes.cend(); ++iter)
  {
    std::cout << iter->first << " : " << iter->second << '\n';
  }
  std::cout << std::endl;
  return 0;
}
Last edited on
Topic archived. No new replies allowed.