Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to C++ and I am writing a program that will simulate a bunny colony. The program will automatically add them, and give them names, gender, age, etc. I am slowly adding in features to the bunnies and I am starting with the names first. When I compile and run the program I get this output, any advice on how I can resolve this issue?

The output:
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


What I have tried:

C++
#include <iostream>
#include <string>
#include <ctime> 
#include <vector>
#include <cstdlib>

using namespace std;

//void setSex( void );
char getSex();
//void setColor( void );
string getColor();
//void setAge( void );
int getAge();
//void setName( void );
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
{
	char sex;
	string color;
	int age;
	string name;

	bool radioactive_mutant_vampire_bunny;
	
	BunnyData()
	{
	//srand( time( 0 ) );

		setSex();
		setColor();
		setAge();
		setName();
	}
	
	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()
	{
		//color = possibleColors[ 0 + rand() % POSSIBLE_COLORS ];
	}

	string getColor() 
	{
		return color;
	}

	void setAge()
	{
		age = 0;
	}

	int getAge() 
	{
		return age;
	}

	void setName()
	{
		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;
}
Posted
Updated 17-Mar-17 22:40pm
Comments
Richard MacCutchan 18-Mar-17 5:06am    
You declare some methods that have no connection to your structure and should be removed from your code. Also, when you create each Bunny object you do not initialise any of its properties.

Use the debugger: put a breakpoint on the first line of Main, and step through your program. At each step, work out in advance what you expect to happen,and when it's done, check if that was exactly what it did. If it was, fine - move on to the next line. If not, why not? What did happen, and how does it differ from what you expected?

We aren't here to do that for you, it's part of your task. So it's time for you to learn a new and useful skill: debugging!
 
Share this answer
 
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900