Jump to content
  • Advertisement
Sign in to follow this  
emforce

C++ need help! functions

This topic is 3387 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi guys trying to re-write one of my current games so that the code is easier to read and easier to modify but i am stuck.

#include <iostream>

int choice;
double combat_options();
int hit_chance;
double monster_health = 50;
double player_damage_crushing = 15;
double combat();



int main()
{
	using namespace std;
	cout << " hello and welcome to player vs monster ";
	cout << " You are a warrior and suddenly a monster jumps out and attacks you! ";
	intro();
	combat_options();
	combat();


	cin.get();
	return 0;
}

double combat_options()
{
	using namespace std;
	
	cout << " what do you do? \n"
		<< " 1) crushing blow ";
	return choice;

	

}
double combat()
{
	using namespace std;
	if (hit_chance % 100 < 75)
	{
		cout << " you have succesfully hit the monster for crushing blow";
		monster_health -= player_damage_crushing;
		cout << "Monsters health :" << monster_health;
	}
	else
	{
		cout << " you have missed the monster... ";
	}	

	return monster_health;

}



please help me :D can someone show me how to incorporate a do{ } while() loop so that it repeats combat options untill monster or player is dead?

Share this post


Link to post
Share on other sites
Advertisement
the syntax goes like so:

do { <things-to-do> } while (cond);


with cond being the condition, which is of type "int", so it can be read as code]do { <things-to-do> } while (0 != (cond));

(in C and C++, this is the same for all conditions).

Note that the <things-to-do> are executed once before the condition is tested. Note that the other form is

while (cond) { <things-to-do> }


Here, the condition is always tested first, and only after cond!=0 or cond==true, the <things-to-do> are executed.

In other words, in the while(){} form (called a "pre-test loop"*), the body is executed at least 0 times, in the do{}while() form (called a "post-test loop"*), the body is executed at least 1 time.


*: Anecdote: In german, we say "head controlled loop" ("kopfgesteuerte Schleife") and "foot controlled loop" ("fußgesteuerte Schleife")

Share this post


Link to post
Share on other sites
Quote:
Original post by phresnel
with cond being the condition, which is of type "int"

In C++ it's bool.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!