为什么我的程序没有提供所需的输出? [英] Why is my program not giving the desired output?

查看:116
本文介绍了为什么我的程序没有提供所需的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++的新手,我正在编写一个模拟兔子殖民地的程序。该程序将自动添加它们,并给它们名称,性别,年龄等。我正在慢慢添加功能到兔子,我首先从名称开始。当我编译并运行程序时,我得到了这个输出,关于如何解决这个问题的任何建议?



输出:

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:

#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;
}

推荐答案

使用调试器:在Main的第一行放置一个断点,然后逐步执行程序。在每个步骤中,提前计算出您期望发生的事情,一旦完成,检查它是否完全正是如此。如果是的话,很好 - 继续下一行。如果没有,为什么不呢?发生了什么,它与你的期望有什么不同?



我们不是来为你做这件事,这是你的任务的一部分。所以现在是时候学习一门新的有用技能了:调试!
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!


当你不理解你的代码在做什么或为什么它做它做的时候,答案是 debugger

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。
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.


这篇关于为什么我的程序没有提供所需的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆