-
March 30th, 2017, 09:14 PM
#1
[RESOLVED] Beginner needs help
Why does my program crash. Also why does HE's(health) value so ridiculously high? One more question, why is at (attack) equal to 1 all the time?
My code:
Code:
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
using namespace std;
int randVal(int min,int max){
return (rand() % (max - min + 1) + min);
}
int main(){
srand(time(NULL));
int bb1,bb2,bb3,hh,aa,HE,ll,tp,na,at,aaplus; //variables for monster one
int bb4,bb5,bb6,hh2,aa2,HE2,ll2,tp2,na2,at2,aaplus2; //variables for monster two
/* bb one through six are body
hh is head
aa is armour
at is attack
HE is health
ll is level
tp is type
na is name */
string Mhead[] = {"( 0 ^ 0 )","| 0 * 0 |","( =^_^= )","( >_< ) ","(<@> <@>)"};
string Mbody1[] = {"1#####1","[[[ ]]]","$ $ ","| ^||^ |"};
string Mbody2[] = {"1#####1","[[[ ]]]","$ $ ","| ^||^ |"};
string Mbody3[] = {"1#####1","[[[ ]]]","| | "," _/|_ "};
string Mname[] = {"Abra","Aipom","Amaura","Arbok","Archen","Aron","Bagon","Beedrill","Beldum","Bayleef","baltoy","Cacnea","Cascoon","Delphox","CHARIZARD",
"Deino","Deerling","Ditto","Dragonair","Drampa","Drowzee","Dustox","Electrabuzz","Emboar","Fearow","Golem","Grimer","Guzzlord","Hypno","Inkay","Invysaur",
"Jolteon","Jynx","Koffinf","Lapras","Latias","Latios","Charmander","Mew","Nidioking","Nidoqueen","Onix","Pichu","Pikachu"};
string Mtype[] = {"Fire","Water","Rock","Air"};
ll = randVal(1,100);
cout<<" input 0,1,2,3, or 4 \n";
cin>>hh;
if(hh < 0 || hh > 4){
cout<<" INVALID INPUT!! re-enter \n";
cin>>hh;
if(hh < 0 || hh > 4){
cout<<" really...";
sleep(3);
return 0;
}
}
cout<<" Now, input 0,1,2, or 3!\n";
cin>>bb1;
if(bb1 < 0 || bb1 > 3){
cout<<" INVALID INPUT!! re-enter \n";
cin>>bb1;
if(bb1 < 0 || bb1 > 3){
cout<<" really...";
sleep(3);
return 0;
}
}
cout<<" again \n";
cin>>bb2;
if(bb2 < 0 || bb2 > 3){
cout<<" INVALID INPUT!! re-enter \n";
cin>>bb2;
if(bb2 < 0 || bb2 > 3){
cout<<"really...";
sleep(3);
return 0;
}
}
cout<<" once more... \n";
cin>>bb3;
if(bb3 < 0 || bb3 > 3){
cout<<" INVALID INPUT!! re-enter \n";
cin>>bb3;
if(bb3 < 0 || bb3 > 3){
cout<< "really...";
sleep(3);
return 0;
}
}
//HEADS - hh
if(hh == 0){
tp = 0;
}
if(hh == 1){
tp == 0;
}
if(hh == 2){
tp == 1;
}
if(hh == 3){
tp == 2;
}
if(hh == 4){
tp == 3;
}
//BODY
//HEALTH - bb1
//HEAVY
if(bb1 == 0 || bb1 == 1){
HE == randVal(1,1150);
}
//LIGHT
if(bb1 == 2 || bb1 == 3){
HE == randVal(1,1000);
}
//ARMOUR - bb2 && bb3
//light
if(bb2 == 2 || bb2 == 3){
aa == randVal(0,450);
}
//HEAVY
if(bb2 == 0 || bb2 == 1){
aa == randVal(0,500);
}
//ARMOUR BOUNUS
//HEAVY
if(bb3 == 0 || bb3 == 1){
aaplus = randVal(0,100);
aa += aaplus;
}
//LIGHT
if(bb3 == 2 || bb3 == 3){
aaplus = randVal(0,50);
aa += aaplus;
}
//ATTACK - AT
//HEAVY
if(bb3 == 0 || bb3 == 1){
at == randVal(1,1000);
}
if(bb3 == 2 || bb3 == 3){
at == randVal(1,1150);
}
cout<<"-------------------- \n";
cout<<" YOUR MONSTER \n \n";
cout<<Mhead[hh]<<endl;
cout<<Mbody1[bb1]<<endl<<Mbody2[bb2]<<endl<<Mbody3[bb3]<<endl<<endl;
cout<<" Name: "<<Mname[na]<<endl;
cout<<" Level: "<<ll<<endl;
cout<<" Type: "<<Mtype[tp]<<endl;
cout<<" Health: "<<HE<<endl;
cout<<" Armour: "<<aa<<endl;
cout<<" Attack: "<<at<<endl;
sleep(4);
cout<<"-------------------- \n";
cout<<" HOSTILE MONSTER \n \n";
ll2 = randVal(1,100);
bb4 = randVal(0,3);
bb5 = randVal(0,3);
bb6 = randVal(0,3);
hh2 = randVal(0,4);
//HEADS - hh
if(hh ==0){
tp == 0;
}
if(hh = 1){
tp == 0;
}
if(hh = 2){
tp == 1;
}
if(hh == 3){
tp == 2;
}
if(hh == 4){
tp == 3;
}
//BODY
//HEALTH - bb4
//HEAVY
if(bb4 == 0 || bb4 == 1){
HE2 == randVal(1,1150);
}
//LIGHT
if(bb4 == 2 || bb4 == 3){
HE2 == randVal(1,1000);
}
//ARMOUR - bb5 && bb6
//light
if(bb5 == 2 || bb5 == 3){
aa2 == randVal(0,450);
}
//HEAVY
if(bb5 == 0 || bb5 == 1){
aa2 = randVal(0,500);
}
//ARMOUR BOUNUS
//HEAVY
if(bb6 == 0 || bb6 == 1){
aaplus2 = randVal(0,100);
aa2 += aaplus2;
}
//LIGHT
if(bb6 == 2 || bb6 == 3){
aaplus2 = randVal(0,50);
aa2 +=+ aaplus;
}
//ATTACK - AT
//HEAVY
if(bb6 == 0 || bb6 == 1){
at2 == randVal(1,1000);
}
if(bb6 == 2 || bb6 == 3){
at2 == randVal(1,1150);
}
cout<<Mhead[hh2]<<endl;
cout<<Mbody1[bb4]<<endl<<Mbody2[bb5]<<endl<<Mbody3[bb6]<<endl<<endl;
cout<<" Name: "<<Mname[na2]<<endl;
cout<<" Level: "<<ll2<<endl;
cout<<" Type: "<<Mtype[tp2]<<endl;
cout<<" Health: "<<HE2<<endl;
cout<<" Armour: "<<aa2<<endl;
cout<<" Attack: "<<at2<<endl;
sleep(4);
cout<<"-------------------- \n";
cout<<" !!!FIGHT!!! \n";
cout<<Mhead[hh]<<" "<<Mhead[hh2]<<endl;
cout<<Mbody1[bb1]<<" "<<Mbody1[bb4]<<endl;
cout<<Mbody2[bb2]<<" vs "<<Mbody2[bb5]<<endl;
cout<<Mbody3[bb3]<<" "<<Mbody3[bb6]<<endl;
HE += aa;
HE2 += aa2;
HE += aa;
HE2 += aa2;
while(HE > 0 && HE2 > 0){
sleep(3);
cout<<endl;
if(tp == tp2){
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 1 && tp2 == 2){
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 1 && tp2 == 3){
at2 *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attacl for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 1 && tp2 == 0){
at *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 0 && tp2 == 1){
at2 *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 0 && tp2 == 2){
at *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 0 && tp2 == 3){
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 2 && tp2 == 0){
at2 *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 2 && tp2 == 1){
at2 *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 2 && tp2 == 3){
at *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 3 && tp2 == 0){
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 3 && tp2 == 1){
at *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
if(tp == 3 && tp2 == 2){
at2 *= 1.2;
HE -= at2;
HE2 -= at;
cout<<" You used "<<Mtype[tp]<<" attack for "<<at<<endl<<endl;
cout<<" Their health is "<<HE2<<endl;
sleep(1);
cout<<" They used "<<Mtype[tp2]<<" attack for "<<at2<<endl<<endl;
cout<<" Your health is "<<HE<<endl;
sleep(2);
}
}
cout<<endl;
if(HE < 0 && HE2 > 0){
cout<<" YOUR MONSTER LOST \n";
cout<<" Their health is "<<HE2<<endl;
}
else if(HE2 < 0 && HE > 0){
cout<<" YOUR MONSTER WON!!!! \n";
cout<<" Your health is "<<HE<<endl;
}
else{
cout<<"!DRAW! \n";
}
string endGAME;
cout<<endl<<"type end to quit \n";
cin>>endGAME;
return 0;
}
-
March 31st, 2017, 04:46 AM
#2
Re: Beginner needs help
A couple of comments
its better to have meaningful names for variables rather than names like hh and a comment that hh is head. My not call the variable head?
its good practice to initialise variables to a known value when they are defined. if they are not so initialised they initial value is unknown and is whatever happens to be in memory at the location the compiler allocates for the variable. If a variable's value seems extremely high at some point it's probably because it is being used without first being initialised.
Some of the code can be shortened. eg
Code:
if(hh == 0){
tp = 0;
}
if(hh == 1){
tp == 0;
}
if(hh == 2){
tp == 1;
}
if(hh == 3){
tp == 2;
}
if(hh == 4){
tp == 3;
}
becomes
Code:
tp = (hh > 1) ? hh - 1 : 0;
note that in above tp is not always being assigned as == is sometimes used rather than =. Also in below HE is not being set but is used for a comparison - as == is used rather than = !
Code:
if(bb1 == 0 || bb1 == 1){
HE == randVal(1,1150);
}
//LIGHT
if(bb1 == 2 || bb1 == 3){
HE == randVal(1,1000);
}
becomes
Code:
HE = (bb1 > 1) ? randVal(1, 1000) : randVal(1, 1150);
and similarly for other parts of the code where == is used rather than =.
In cases like this, it is useful to use the debugger to trace through the code to see what is happening. If you haven't uses the debugger, now is a good time to learn.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 31st, 2017, 08:27 AM
#3
Re: Beginner needs help
Originally Posted by TheRedSpy
Why does my program crash.
Please, define "crash"! Where, when and how does it crash?
Originally Posted by TheRedSpy
... Also why does HE's(health) value so ridiculously high? One more question, why is at (attack) equal to 1 all the time?
You have to debug your code to find out why...
Victor Nijegorodov
-
March 31st, 2017, 09:01 AM
#4
Re: Beginner needs help
I actually tried my debugger, and I get:
Code:
=> 0x00000000004728f0 <+0>: mov rdx,QWORD PTR [rdx]
0x00000000004728f3 <+3>: mov r8,QWORD PTR [rdx-0x18]
0x00000000004728f7 <+7>: jmp 0x46f290 <_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_x>
0x00000000004728fc <+12>: nop
0x00000000004728fd <+13>: nop
0x00000000004728fe <+14>: nop
0x00000000004728ff <+15>: nop
Also, by crash I mean the program stops before finishing. I forgot to add those two things in the post.
-
March 31st, 2017, 09:42 AM
#5
Re: Beginner needs help
Originally Posted by TheRedSpy
I actually tried my debugger, and I get:
Code:
=> 0x00000000004728f0 <+0>: mov rdx,QWORD PTR [rdx]
0x00000000004728f3 <+3>: mov r8,QWORD PTR [rdx-0x18]
0x00000000004728f7 <+7>: jmp 0x46f290 <_ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_x>
0x00000000004728fc <+12>: nop
0x00000000004728fd <+13>: nop
0x00000000004728fe <+14>: nop
0x00000000004728ff <+15>: nop
Also, by crash I mean the program stops before finishing. I forgot to add those two things in the post.
And what line of your code does it correspond to?
Victor Nijegorodov
-
March 31st, 2017, 02:20 PM
#6
Re: Beginner needs help
I don't know, that just popped up when I did it. My code is working now though. I stilling learning to use the debugger.
-
March 31st, 2017, 10:12 AM
#7
Re: Beginner needs help
Have you altered the code re the issues described in post #2? Have you used the debugger on your revised code to step through the code and watch the values of the variables as they change as you step through the code? When you do that you'll see where the execution path through the code and the contents of the variables becomes different to what you expect. Then you'll know whereabouts in the program lies the problem(s). Once you know that if you still need advice then re-post the revised code with some comment re where the problem lies and we'll be able to further advise.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
March 31st, 2017, 02:22 PM
#8
Re: Beginner needs help
Actually, your other post fixed my code! Thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|