3 Replies - 12185 Views - Last Post: 07 July 2009 - 09:58 PM Rate Topic: -----

#1 kaaie  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 93
  • Joined: 22-June 09

Floating point exception (core dumped)

Post icon  Posted 07 July 2009 - 09:32 PM

I have created a simple RPG and whenever I compile it, run it, the enter 1 as my first selection(to go to the monster) it gives me the floating point exception (core dumped) error. I've tried everything to solve it but can't find the problem.

Here is the code:
#include <iostream>
#include <cstdlib>
#include <ctime>

struct Characters
{
	int nLevel;
	int nHealth;
	int nAttack;
	int nDefence;
};

Characters sPlayer;
Characters sMonster;

void monster()
{
	using namespace std;
	srand(time(0));
	sMonster.nLevel = sPlayer.nLevel - 1 - (rand() % 2);//So that the monster level is at least 1 level smaller than the user level
	sMonster.nHealth = sMonster.nLevel * 100;				//TODO: Add an xp system to advance levels
	sMonster.nAttack = sMonster.nLevel * 10;
	sMonster.nDefence = sMonster.nLevel * 10;
	int nInflict = sPlayer.nAttack - sMonster.nDefence;
	if (nInflict < 0)
		nInflict = 0;
	int nReceive = sMonster.nAttack - sPlayer.nDefence;
	if (nReceive < 0)
		nReceive = 0;
	do
	{
		nInflict = (rand() % (nInflict));
		sMonster.nHealth = sMonster.nHealth - nInflict;
		nReceive = (rand() % (nReceive));
		sPlayer.nHealth = sPlayer.nHealth - nReceive;
		cout << "You hit " << nInflict << "!\t\t(Monster HP:  " << sMonster.nHealth << "\n";
		cout << "You get " <<nReceive << "damage!\t\t(Your HP: " << sPlayer.nHealth << "\n\n";
	}
	while (sMonster.nHealth < 0 || sPlayer.nHealth < 0);
	if (sMonster.nHealth < 0)
		cout << "You defeated the monster!\n\n";
	else
		cout << "You are dead!\n\n";
}

int main()
{
	using namespace std;
	srand(time(0));
	sPlayer.nLevel = 5;			//Starting level
	cout << "Welcome to a simple RPG!\n\n";
	int nChoice;
	do
	{
		sPlayer.nHealth = sPlayer.nLevel * 100;
		sPlayer.nAttack = sPlayer.nLevel * 10;
		sPlayer.nDefence = sPlayer.nLevel * 10;
		cout << "Please select your choice: ";
		cin >> nChoice;
		switch (nChoice)
		{
		case 1:
			cout << "A monster has apeared!\n";
			monster();
			break;
		case 2:
			cout << "Random chance!\n"; //To be implemented
			break;
		default:
			cout << "Invalid choice!\n\n";
		}
	}
	while (sPlayer.nHealth > 0);
	return 0;
}



Any help would be appreciated!
thanks,

kaaie

Is This A Good Question/Topic? 0
  • +

Replies To: Floating point exception (core dumped)

#2 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Floating point exception (core dumped)

Posted 07 July 2009 - 09:37 PM

	do
	{
		nInflict = (rand() % (nInflict));
		sMonster.nHealth = sMonster.nHealth - nInflict;
		nReceive = (rand() % (nReceive));
		sPlayer.nHealth = sPlayer.nHealth - nReceive;
		cout << "You hit " << nInflict << "!\t\t(Monster HP:  " << sMonster.nHealth << "\n";
		cout << "You get " <<nReceive << "damage!\t\t(Your HP: " << sPlayer.nHealth << "\n\n";
	}
	while (sMonster.nHealth < 0 || sPlayer.nHealth < 0);



I think your while conditions should be


while (sMonster.nHealth > 0 && sPlayer.nHealth > 0);
Was This Post Helpful? 0
  • +
  • -

#3 mblazek  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 05-March 09

Re: Floating point exception (core dumped)

Posted 07 July 2009 - 09:39 PM

Two things I see,

The using namespace std; seems out of place to me, and somewhat unecessary for where they are. I could understand them after a #include:

#include <iostream>
using namespace std;



The other thing is that your sMonster character is not intialized, none of the values have been intialized so you may been getting that error because of this. Try setting some defaults values ofr sMonster before you call its members.

Edited -- Never mind I see where you intialize the values sorry about that.

This post has been edited by mblazek: 07 July 2009 - 09:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Floating point exception (core dumped)

Posted 07 July 2009 - 09:58 PM

Debugged and found it.

nReceive = (rand() % (nReceive));


Here, nReceive has a value of 0. Can't divide by zero.

Fix that and the mod before it, what mblazek and I said and that should help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1