我是C ++的新手,我正在编写一个c ++程序来模拟一群兔子。该程序将自动添加它们,给它们命名,年龄等。我有点困惑为什么我的程序给我这个。我研究了几个想法,但似乎仍然不知所措。到目前为止,我的尝试一直专注于获得一个名称来显示。当程序执行变量名称,性别,颜色时,不返回任何内容。
这是代码:
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include <cstdlib>
using namespace std;
void setSex();
char getSex();
void setColor(string color);
string getColor();
void setAge(int age);
int getAge();
void setName(string name);
string getName();
void printBunny();
int randomGeneration(int x);
//static std::string POSSIBLE_NAMES = 18;
//static std::string POSSIBLE_COLORS = 4;
static std::string possibleNames[] ={
"Jen",
"Alex",
"Janice",
"Tom",
"Bob",
"Cassie",
"Louis",
"Frank",
"Bugs",
"Daffy",
"Mickey",
"Minnie",
"Pluto",
"Venus",
"Topanga",
"Corey",
"Francis",
"London",
};
static std::string possibleColors[] ={
"White",
"Brown",
"Black",
"Spotted"
};
struct Bunny
{
public:
string name;
int age;
string color;
char sex;
Bunny(){
name = "";
age = 0;
color = "";
}
Bunny(string name, string color, int age){
name = name;
color = color;
age = age;
setName(name);
setColor(color);
setAge(age);
}
//radioactive_mutant_vampire_bunny(radioactive_mutant_vampire_bunny)
/*bool sex;
std::string name, color;
int age;
//string name;
bool radioactive_mutant_vampire_bunny;*/
int randomGeneration(int x){
return rand() % x;
}
void setSex()
{
int randomNumber = 1 + rand() % 2;
( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
}
char getSex()
{
return sex;
}
void setColor(string color)
{
//color = possibleColors[ 0 + rand() % POSSIBLE_COLORS ];
}
string getColor()
{
return color;
}
void setAge(int age)
{
age = 0;
}
int getAge()
{
return age;
}
void setName(string name)
{
int i = randomGeneration(18);
name = possibleNames[i];
//name = possibleNames[ 0 + rand() % POSSIBLE_NAMES ];
}
string getName()
{
return name;
}
void printBunny()
{
cout << "Name: " << getName() << endl;
cout << "Sex: " << getSex() << endl;
cout << "Color: " << getColor() << endl;
cout << "Age: " << getAge() << endl;
}
};
int main()
{
vector< Bunny > colony;
cout << "Welcome to Bunny Graduation!" << endl << endl;
for( int i = 0; i < 5; ++i )
{
colony.push_back( Bunny() );
}
for( int i = 0; i < 5; ++i )
{
colony[ i ].printBunny();
cout << endl;
}
return 0;
}
这是我收到的输出:
enter code here
Welcome to Bunny Graduation!
Name:
Sex: ▒
Color:
Age: 0
Name:
Sex: ▒
Color:
Age: 0
Name:
Sex: ▒
Color:
Age: 0
Name:
Sex: ▒
Color:
Age: 0
Name:
Sex: ▒
Color:
Age: 0
答案 0 :(得分:0)
你的代码在很多方面被打破了,我宁愿给出某种实现,希望最终结果符合你的期望。
请注意,此代码不代表实际的C ++ 1x标准,仅出于教育目的而发布。
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
const int POSSIBLE_NAMES = 18;
const int POSSIBLE_COLORS = 4;
static std::string possibleNames[] ={
"Jen",
"Alex",
"Janice",
"Tom",
"Bob",
"Cassie",
"Louis",
"Frank",
"Bugs",
"Daffy",
"Mickey",
"Minnie",
"Pluto",
"Venus",
"Topanga",
"Corey",
"Francis",
"London",
};
static std::string possibleColors[] ={
"White",
"Brown",
"Black",
"Spotted"
};
struct Bunny
{
public:
string name;
int age;
string color;
char sex;
Bunny(){
setSex();
setColor();
setAge(0);
setName();
}
int randomGeneration(int x){
return rand() % x;
}
void setSex()
{
int randomNumber = randomGeneration(2);
( randomNumber == 1 ) ? sex = 'm' : sex = 'f';
}
char getSex()
{
return sex;
}
void setColor()
{
int randomNumber = randomGeneration(POSSIBLE_COLORS);
color = possibleColors[randomNumber];
}
string getColor()
{
return color;
}
void setAge(int age)
{
this->age = age;
}
int getAge()
{
return age;
}
void setName()
{
int i = randomGeneration(POSSIBLE_NAMES);
name = possibleNames[i];
}
string getName()
{
return name;
}
void printBunny()
{
cout << "Name: " << getName() << endl;
cout << "Sex: " << getSex() << endl;
cout << "Color: " << getColor() << endl;
cout << "Age: " << getAge() << endl;
}
};
int main()
{
vector< Bunny > colony;
cout << "Welcome to Bunny Graduation!" << endl << endl;
for( int i = 0; i < 5; ++i )
{
colony.push_back( Bunny() );
}
for( int i = 0; i < 5; ++i )
{
colony[ i ].printBunny();
cout << endl;
}
return 0;
}