Sign in to follow this  
emforce

C++ need help! functions

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
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));[/code]

(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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this