I am working through a c++ problem set for fun. I am creating a loop which will add objects to a class with random characteristics. One of these random characteristics is the object name. I have a program which randomly generates names for the class object, but I cannot figure out how to assign those names to the class objects.
Here is my class and it's constructor:
#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include <string>
#include <sstream>
#include <array>
#include <time.h>
#include <conio.h>
class Bunny
{
public:
char fname;
int sex, age, color, status;
Bunny(int, int, int, int);
int s() { return (sex); }
int a() { return (age); }
int c() { return(color);}
int st() { return (status);}
};
Bunny::Bunny(int s, int a, int c, int st)
{
sex = s;
age = a;
color = c;
status = st;
}
This is the Loop which generates "bunny" information.
std::string name;
for (n = 0; n < 1; n++)
{
int num = std::rand() % 5;
int s = std::rand() % 2;
std::string f = "Susan"; //this is normally a function which randomly gives names
name = f;
int a = 0;
int c = std::rand() % 5;
int st;
if (rand() % 50 == 43) st = 1; else st = 0;
Bunny name(s, a, c, st);
}
if it was working right it should make a member like this:
Bunny Susan(1, 0, 2, 0);
This doesn't work because it creates a class object called "name" rather than "Susan". How can I assign a class object a name through a variable rather than manually assigning the name in the code.
std::mapto make strings like"Susan"to data.