2 Replies - 656 Views - Last Post: 22 July 2009 - 06:12 PM Rate Topic: -----

#1 ASE   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-July 09

Help with while and for loops?

Posted 22 July 2009 - 05:29 PM

I need help with the loops part of my homework. I've spent a good three hours on it. Any help is greatly appreciated.Thanks. The rules are below.
I just can't understand how to create loops to fit the needs. Please explain how.

1) while loop to indicate when either the player or the monster has died in battle
2) for loop to allow the battle to continue for at least 3 rounds.

Variables: MonsterHitPoints = the life of the monster
MonsterStrength = strength of monster

hitPoints = life of the Hero
strength = strength of hero

You can use these variables to refresh the monster and hero status per battle if you like to work more. Please use hitPoints and MonsterHitPoints for the while loop to determine if the hero or Monster has died
#include <iostream>
#include <string>

using namespace std;

int main() {
	
	//Monster variables //
	string MonsterName = "Virus";
	int MonsterHitPoints = 20;
	int MonsterStrength = 5;
	
	//Hero Variables //1
	string HeroName = "Kaspersky Anti-Virus";
	int hitPoints = 30;
	int strength = 5;
	
	// Menu Variable //
	string menu = "What are you going to do " + HeroName + "\n 1 - attack \n 2 - defend \n 3 - run away ";
	
	//action variable //
	int action;
	

	  
	  
	  cout << "Prepare for battle!!"<< endl;;
	  
	  MonsterHitPoints = 20;
	  

		cout << menu << endl;
		cin >> action; 
		if (action == 1){
		  MonsterHitPoints = MonsterHitPoints - strength;
		  cout <<"You attacked the " << MonsterName << endl;
		  cout <<"Monster life is now at " << MonsterHitPoints << endl;;
		}
		else if (action == 2){
		  hitPoints = hitPoints - MonsterStrength;	  
		  cout <<"You defended, " << MonsterName  << " has attacked you!" <<endl;
		  cout <<"Your life is now at " << hitPoints << endl;
		}
		else if (action == 3){
		  cout <<"You cant run!" << endl;
		}
	   
	  
	   

	
	int stop;
	cin >> stop;
   //****************** END MAIN PROGRAM HERE ****************************//
} 


This post has been edited by ASE: 22 July 2009 - 05:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with while and for loops?

#2 gareth00   User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 49
  • Joined: 08-May 09

Re: Help with while and for loops?

Posted 22 July 2009 - 06:06 PM

Well for the first part, the while loop would check if the life of the hero is greater than 0. If it is, then the battle can continue.
Was This Post Helpful? 0
  • +
  • -

#3 deery5000   User is offline

  • D.I.C Lover

Reputation: 87
  • View blog
  • Posts: 1,097
  • Joined: 09-May 09

Re: Help with while and for loops?

Posted 22 July 2009 - 06:12 PM

Not too sure that this is what your looking for but i hope it helps

#include "stdafx.h"


#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int main() {

	//Monster variables //
	string MonsterName = "Virus";
	int MonsterHitPoints = 20;
	int MonsterStrength = 5;

	//Hero Variables //1
	string HeroName = "Kaspersky Anti-Virus";
	int hitPoints = 30;
	int strength = 5;

	// Menu Variable //
	string menu = "What are you going to do " + HeroName + "\n 1 - attack \n 2 - defend \n 3 - run away ";

	//action variable //
	int action;

	cout << "Prepare for battle!!"<< endl;;

	MonsterHitPoints = 20;

	int monsterScore = 0;
	int humanScore = 0;

	// Play game 3 times 
	for(int i = 0; i < 3; i++) 
	{

		while(MonsterHitPoints > 0)
		{
			cout << menu << endl;
			cin >> action; 

			if (action == 1){
				MonsterHitPoints = MonsterHitPoints - strength;
				cout <<"You attacked the " << MonsterName << endl << endl;
				cout <<"Monster life is now at " << MonsterHitPoints << endl;;
			}
			else if (action == 2){
				hitPoints = hitPoints - MonsterStrength;	  
				cout <<"You defended, " << MonsterName  << " has attacked you!" <<endl << endl;;
				cout <<"Your life is now at " << hitPoints << endl;
			}
			else if (action == 3){
				cout <<"You cant run!" << endl;
			}


		}	 


		if (MonsterHitPoints == 0)
		{
			cout << " Well Done! you defeated the Monster" << endl << endl;;
			humanScore++;
		}
		else
		{
			cout << "you have been defeated" << endl << endl;
			monsterScore++;
		}

		MonsterHitPoints = 20;
	}
	cout << " Monster Score:  " << monsterScore << endl;
	cout << " Human Score  :  " << humanScore << endl;

	_getch();  // pauses screen 		
} 



Kevin

This post has been edited by deery5000: 22 July 2009 - 06:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1