Hello everyone I am trying to declare a array of 10 objects with values attached to them by a random number generator. After declaring a array of 10 accounts how do i make their id numbers 0-9 and generate a balance between 100-10000 and a interest rate of 1.5-4.5. Then after these values are assigned to each object what is the syntax for getting them to use them in further calculations. I do not want the values to reset until after the program terminates.
#include <random>
#include <iostream>
// An account has an id, a balance, and an interest rate
class Account {
public:
Account() {}
Account(int id, int balance, double interest_rate)
: id(id), balance(balance), interest_rate(interest_rate)
{}
int id;
int balance;
double interest_rate;
};
int main()
{
// from <random>, search each class name to know more
// http://www.cplusplus.com/reference/random/
std::default_random_engine generator;
std::uniform_int_distribution<int> random_balance(100, 10000);
std::uniform_real_distribution<double> random_interest_rate(1.5, 4.5);
constint N = 10;
Account accounts[N];
for (int i = 0; i < N; i++)
{
// set id, and random values for balance and interest rates
int id = i;
int balance = random_balance(generator);
int interest_rate = random_interest_rate(generator);
// set account
accounts[i] = Account(id, balance, interest_rate);
}
// do further processing.
for (int i = 0; i < N; i++)
{
std::cout << accounts[i].balance << std::endl;
}
}
@OP Please stick to a single thread. So far you have 3 threads on essentially the same topic. Multiple posting wastes peoples time in losing context, duplication of volunteer effort and is not good for you. Please be courteous and pick just one thread now and close off the others with a green tick to show they are complete and that there is no need to revisit.