Hello. I know it would be easier to create an int array, but I want the program to create integers as the user requests. For example, if the user enters 4, the program creates 4 integers with the names input. Each integer is better as an individual integer, not an element in an array.I'm not sure if this is a possibility with C++. Any responses would be greatly appreciated. Thank you.
-Have your program prompt the user for the variable names and values and store them someplace (maybe an array? lol)
-Open up the source code of your file (you want this to be a separate copy of the same source code), add in the newly named integers into it, save the file.
-Call up the compiler to compile that new source file.
-Exit current program and run new program with their new integers in .
You could probably fake it using a std::map or std::unordered_map (C++11) where the keys are the variable names and the values are, well, the values of the variables.
Kinda like this:
// Use a C++11 compiler, please!
#include <iostream>
#include <string>
#include <unordered_map>
int main()
{
std::cout << "Enter some variable names and values:\n";
std::unordered_map<std::string, int> vars;
while (true)
{
std::string name;
int value;
std::cout << ">> ";
if (!(std::cin >> name >> value))
break;
vars[name] = value;
}
std::cout << "\nHere are your variables (sorry, they might be in a different order):\n";
for (constauto& p : vars)
std::cout << p.first << " = " << p.second << '\n';
}
Possible output:
Enter some variable names and values:
>> myInt 14
>> blah -3
>> yoloSwag 69
>> mostDescriptiveIntegerNameInTheHistoryOfEver -101
>> somethingBlahBlah 5
>> ^Z
Here are your variables (sorry, they might be in a different order):
somethingBlahBlah = 5
mostDescriptiveIntegerNameInTheHistoryOfEver = -101
yoloSwag = 69
blah = -3
myInt = 14