Loop won't end help

Hi everyone, long time reader first-time poster but I don't get why this loop won't end. I've tried everything for the last couple days that I can think of, basically just making a simple game and I want the loop to stop when my health objects are dead but it just keeps going lol even after game over screens.
Ideally, I'd like the object to be able to break the loop so I don't have to program it in everytime.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <dos.h>
#include <dir.h>
#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;
void SetColor(int ForgC)
{
WORD wColor;

HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;

//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
void ClearConsoleToColors(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
//Get the handle to the current output buffer...
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This is used to reset the carat/cursor to the top left.
COORD coord = {0, 0};
//A return value... indicating how many chars were written
// not used but we need to capture this since it will be
// written anyway (passing NULL causes an access violation).
DWORD count;

//This is a structure containing all of the console info
// it is used here to find the size of the console.
CONSOLE_SCREEN_BUFFER_INFO csbi;
//Here we will set the current color
SetConsoleTextAttribute(hStdOut, wColor);
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//This fills the buffer with a given character (in this case 32=space).
FillConsoleOutputCharacter(hStdOut, (TCHAR) 32, csbi.dwSize.X * csbi.dwSize.Y, coord, &count);

FillConsoleOutputAttribute(hStdOut, csbi.wAttributes, csbi.dwSize.X * csbi.dwSize.Y, coord, &count );
//This will set our cursor position for the next print statement.
SetConsoleCursorPosition(hStdOut, coord);
}
return;
}

class monster
{
int MD;
int MH = 40;

public:
int getMH()
{
return MH;
}
int setMH(int theMH)
{
MH = theMH;
}
int getMD()
{
return MD;
}
int setMD(int theMD)
{
MD = theMD;
}


};
class weapons: public monster
{
unsigned int spear[2] = {15, 80};
int shield[3] = {8,120,60};/////first number attack, second durabilty, third block
int SD = spear[0];
int SRR = spear[1];
int SPATK;
int SRRD;
int FSD = shield[0];
int FSRR = shield[1];

public:

int getSD(){
return spear[0];
}
int setSD(int theSD)////spear damage
{
SD = theSD;
}
int getSRR()////spear repair rate
{
return spear[1];
}
int setSRR(int theSRR)
{
SRR = theSRR;
}
int SPATCK(int spearattack)
{ setSD(rand()%15);
getSD();
SPATK = getMH() - getSD();
setMH(SPATK);
if(getMH()<0)
{
ClearConsoleToColors(0,14);

SetColor(0);
cout << "\n YOU WIN \n";
system("PAUSE");
return 0;

}
cout << "\nYour spear attacks for " << getSD() << endl;

cout << "Monster has " << getMH() << " Health Left\n";
SRRD = getSRR() - (rand()%5);
setSRR(SRRD);
cout << "Spear durabilty " << getSRR();
}

int getFSD()
{
return shield[0];
}
int setFSD(int theFSD)
{
FSD = theFSD;
}
int getFSRR()
{
return shield[1];
}
int setFSRR(int theFSRR)
{
FSRR = theFSRR;
}


};
class health: public monster
{
int h = 100;

public:
int Dhp;
int geth(){
return h;
}
int seth(int theh)
{
h = theh;
}
int damh(int thedamh)
{ setMD(rand()%15);
thedamh = getMD();
cout << "You Lost " << thedamh << endl;
Dhp = h - thedamh;
seth(Dhp);
if(geth()<=0)
{
cout << "\n GAME OVER \n";
system("PAUSE");
return 0;
}
cout << " HEALTH " << geth() << endl;}
};



int y;
int n;
health HP;
int x;
int d;
int z = 0;
monster MD;
monster MH;
weapons SD;
weapons SRR;
weapons FSD;
weapons FSRR;
int mh = 0;
int srr = 0;
int atr = 0;

int main()

{ srand(time(0));



cout << "Health " << HP.geth() << endl;
cout << "Wild Wombat appears" << endl;
x = 40;
MH.setMH(x);
while(MH.getMH()>0 && HP.geth()>0){

cout << "Use 1 for main attack \n or 2 for shield bash\n";
cin >> y;

if(y<=1)
{
SD.SPATCK(0);
HP.damh(0);}
if(MH.getMH()<=0){
cout << "meow" << endl;
return NULL;
cin >> y;
cout << "work damn you";
} }

}











Oh, and thanks ahead of time for any help; sorry I thought about writing it but forgot to write it.
This is some ugly, ugly code with some pretty bad design decisions. Looks like you've understood the syntax of inheritance, but missed the point; health is a kind of monster? Hard to imagine the design where that makes sense.

The following functions say they return an int, but they don't:
setMH
setMD
setSD
setSRR
setFSD
setFSRR
seth
main (you're returning NULL from main?)
SPATCK
damh

Fix all that.
Ya okay, I didn't void my sets so thats why they werent' returning value
Last edited on
I didn't void the sets in my classes was a big part of it
So I should reverse the health thingy right? So Monster has a type of health? Then put all my health? Monster : Health. Ya okay it makes more sense now that I type it out, thanks.
Monster should HAVE health. Monster should CONTAIN a health value. A monster is not a kind of health, and a health is not a kind of monster.

Monster should not inherit from health.
Health should not inherit from monster.
Last edited on
Topic archived. No new replies allowed.