What is wrong with this code?

Posts 1–11 of 11 · Page 1 of 1
What is wrong with this code?
What is wrong with this code? I'm probably too tired to notice it right now, it's 2 am and I'm nearly blacking out. I wanted to practice c++ cuz i suck so i started to make this little game thingy, but now im sooo tired. Can somebody fix it pl@wkz while I sl33pz0r?

Code:
#include <iostream.h>
#include <fstream.h>
int myhp;
int mysp;
int mydmg;
int ohp;
int osp;
int odmg;
int input; int turns;
void turn()
{myhp+25;mysp++;ohp+25;osp++;turns++;
if ( mysp >= 5 && mysp <= 10 ) {mydmg + 7; }   // determining
if ( mysp >= 11 && mysp <= 20) {mydmg + 12;}   // your
if ( mysp >= 21 && mysp <= 30) {mydmg + 15;}   // damage    
if ( osp >= 5 && osp <= 10 ) {odmg + 5;}   // determining 
if ( osp >= 11 && osp <= 20) {odmg + 7;}   // opponent
if ( osp >= 21 && osp <= 30) {odmg + 9;}   // damage
myhp - odmg;
ohp - mydmg;
return;
}
int main()
{
cout <<"Hello Player."<<endl;
cout <<" The Game Begins. "<<endl;
int myhp = 100;     //initial values
int mysp = 0;       //
int mydmg = 17;     //
int ohp = 100;      //
int osp = 0;        //
int odmg = 15;      //initial values
while (1){cout <<"Enter 1 to start a turn";cin >> input;
if ( input = 1 ){turn();}
if ( input != 1){break;}
cout<<myhp<<endl;cout<<mydmg<<endl;cout<<ohp<<endl;cout<<odmg<<endl; 
if(myhp<ohp){cout<<"You Lose";getchar();break;}
if(ohp<myhp){cout<<"You Win";getchar();break;} 
if(turns>40){break;}
}
cout <<"End of Program."<<endl;
getchar();
return 0;
}
Looks fine to me
Well there is one thing..well first what are you trying to accomplish
Testing your hp to the amount of damage ?
And is it some type of game..
Also what does sp stand for?
Quote Originally Posted by cnttuchme View Post
Looks fine to me
Well there is one thing..well first what are you trying to accomplish
Testing your hp to the amount of damage ?
And is it some type of game..
Also what does sp stand for?
It's like a text based game, i got bored. sp is like special points, except I didn't use them the way most people would. Also, whenever I compile and run this the variables never change. Do I have to include them in my turn() function as parameters? gah screw this I'll check back in with you guys tomorrow, I'm tired as hell. Bye, and thanks for any insight on this.
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>


int myhp;
int mysp;
int mydmg;
int ohp;
int osp;
int odmg;
int input; int turns;
void turn()
{myhp+25;mysp++;ohp+25;osp++;turns++;
if ( mysp >= 5 && mysp <= 10 ) {mydmg + 7; }   // determining
if ( mysp >= 11 && mysp <= 20) {mydmg + 12;}   // your
if ( mysp >= 21 && mysp <= 30) {mydmg + 15;}   // damage    
if ( osp >= 5 && osp <= 10 ) {odmg + 5;}   // determining 
if ( osp >= 11 && osp <= 20) {odmg + 7;}   // opponent
if ( osp >= 21 && osp <= 30) {odmg + 9;}   // damage
myhp - odmg;
ohp - mydmg;
return;
}
int main()
{
cout <<"Hello Player."<<endl;
cout <<" The Game Begins. "<<endl;
int myhp = 100;     //initial values
int mysp = 0;       //
int mydmg = 17;     //
int ohp = 100;      //
int osp = 0;        //
int odmg = 15;      //initial values
while (1){cout <<"Enter 1 to start a turn";cin >> input;
if ( input = 1 ){turn();}
if ( input != 1){break;}
cout<<myhp<<endl;cout<<mydmg<<endl;cout<<ohp<<endl;cout<<odmg<<endl; 
if(myhp<ohp){cout<<"You Lose";getchar();break;}
if(ohp<myhp){cout<<"You Win";getchar();break;} 
if(turns>40){break;}
}
cout <<"End of Program."<<endl;
getchar();
return 0;
}
forgot to include a verry important part XD simple mistake.
Quote Originally Posted by cnttuchme View Post
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>


int myhp;
int mysp;
int mydmg;
int ohp;
int osp;
int odmg;
int input; int turns;
void turn()
{myhp+25;mysp++;ohp+25;osp++;turns++;
if ( mysp >= 5 && mysp <= 10 ) {mydmg + 7; }   // determining
if ( mysp >= 11 && mysp <= 20) {mydmg + 12;}   // your
if ( mysp >= 21 && mysp <= 30) {mydmg + 15;}   // damage    
if ( osp >= 5 && osp <= 10 ) {odmg + 5;}   // determining 
if ( osp >= 11 && osp <= 20) {odmg + 7;}   // opponent
if ( osp >= 21 && osp <= 30) {odmg + 9;}   // damage
myhp - odmg;
ohp - mydmg;
return;
}
int main()
{
cout <<"Hello Player."<<endl;
cout <<" The Game Begins. "<<endl;
int myhp = 100;     //initial values
int mysp = 0;       //
int mydmg = 17;     //
int ohp = 100;      //
int osp = 0;        //
int odmg = 15;      //initial values
while (1){cout <<"Enter 1 to start a turn";cin >> input;
if ( input = 1 ){turn();}
if ( input != 1){break;}
cout<<myhp<<endl;cout<<mydmg<<endl;cout<<ohp<<endl;cout<<odmg<<endl; 
if(myhp<ohp){cout<<"You Lose";getchar();break;}
if(ohp<myhp){cout<<"You Win";getchar();break;} 
if(turns>40){break;}
}
cout <<"End of Program."<<endl;
getchar();
return 0;
}
forgot to include a verry important part XD simple mistake.
That really isn't neccessary to include. The reason you game is flawed is because of all these statements.

Code:
mydmg + 7;
mydmg + 12;
mydmg + 15;
odmg + 5;
odmg + 7;
odmg + 9;
You keep adding and subtracting things, but you never assign a value to anything. So none of these values are saved.

Try doing this: mydmg = 7;
That would help you a lot, there are also a few flaws in the construction of the game, just the design I mean, but its hard to worry about bigger problems like that if your messing up the fundamentals.

Hey! I've got an idea. You might want to buy this book called: Learning C++ by Game programming... or something like that. Basically it teaches you the fundamentals of C++ by making fun little console games.
you were trying to make 2 different values to be same at once
u had to use or || .
Quote Originally Posted by t7ancients View Post
What is wrong with this code? I'm probably too tired to notice it right now, it's 2 am and I'm nearly blacking out. I wanted to practice c++ cuz i suck so i started to make this little game thingy, but now im sooo tired. Can somebody fix it pl@wkz while I sl33pz0r?

Code:
#include <iostream.h>
#include <fstream.h>
int myhp;
int mysp;
int mydmg;
int ohp;
int osp;
int odmg;
int input; int turns;
void turn()
{myhp+25;mysp++;ohp+25;osp++;turns++;
if ( mysp >= 5 ||mysp <= 10 ) {mydmg + 7; }   // determining
if ( mysp >= 11 || mysp <= 20) {mydmg + 12;}   // your
if ( mysp >= 21 || mysp <= 30) {mydmg + 15;}   // damage    
if ( osp >= 5 || osp <= 10 ) {odmg + 5;}   // determining 
if ( osp >= 11 || osp <= 20) {odmg + 7;}   // opponent
if ( osp >= 21 || osp <= 30) {odmg + 9;}   // damage
myhp - odmg;
ohp - mydmg;
return;
}
int main()
{
cout <<"Hello Player."<<endl;
cout <<" The Game Begins. "<<endl;
int myhp = 100;     //initial values
int mysp = 0;       //
int mydmg = 17;     //
int ohp = 100;      //
int osp = 0;        //
int odmg = 15;      //initial values
while (1){cout <<"Enter 1 to start a turn";cin >> input;
if ( input = 1 ){turn();}
if ( input != 1){break;}
cout<<myhp<<endl;cout<<mydmg<<endl;cout<<ohp<<endl;cout<<odmg<<endl; 
if(myhp<ohp){cout<<"You Lose";getchar();break;}
if(ohp<myhp){cout<<"You Win";getchar();break;} 
if(turns>40){break;}
}
cout <<"End of Program."<<endl;
getchar();
return 0;
}
Quote Originally Posted by Matrix_NEO006 View Post
you were trying to make 2 different values to be same at once
u had to use or || .
Code:
if ( mysp >= 21 && mysp <= 30) {mydmg + 15;}   // damage
wouldn't this just work?
because it are not "exact values"
if it is 23 it is greater then 21 and smaller then 30.
Quote Originally Posted by Matrix_NEO006 View Post
you were trying to make 2 different values to be same at once
u had to use or || .
wtf? he needs to use &&, since he wants it to be between those two, not anything(since e.g. 11 is bigger then 5 and 3 is smaller then 10 so it would always pass).
k, thanks guys. I never really learned a lot of c++, I just looked over a lot of source code and tried to figure it out myself. tyvm for the help.
Quote Originally Posted by t7ancients View Post
k, thanks guys. I never really learned a lot of c++, I just looked over a lot of source code and tried to figure it out myself. tyvm for the help.
This is why I reccommend getting a book. Just learning from source code seems like a good idea at first, but the longer you go at the more you will realize you have HUGE gaps in your knowledge. Most source codes for hacks use fairly advanced techniques and rarely are commented to explain how they work because it's assumed that you already know if your using the source.

Oh and lala you are absolutely right. In fact using || instead of && in that situation would have negative combinations. Essentially one if statement would work for an infinite number of values for mysp which is not what you want
For dev-c++

Don't have VC++ on laptop :P

Code:
#include <iostream>
using namespace std;

int myhp = 0;
int mysp = 0;
int mydmg = 0;
int ohp = 0;
int osp = 0;
int odmg = 0;
int input = 0; 
int turns = 0;

void turn()
{
myhp = 25;
mysp++;

ohp = 25;
osp++;
turns++;
if ( mysp >= 5 && mysp <= 10 ) {mydmg + 7; }   // determining
if ( mysp >= 11 && mysp <= 20) {mydmg + 12;}   // your
if ( mysp >= 21 && mysp <= 30) {mydmg + 15;}   // damage    
if ( osp >= 5 && osp <= 10 ) {odmg + 5;}   // determining 
if ( osp >= 11 && osp <= 20) {odmg + 7;}   // opponent
if ( osp >= 21 && osp <= 30) {odmg + 9;}   // damage

myhp - odmg;
ohp - mydmg;
return;
}

int main()
{
cout <<"Hello Player."<<endl;
cout <<" The Game Begins. "<<endl;
int myhp = 100;     //initial values
int mysp = 0;       //
int mydmg = 17;     //
int ohp = 100;      //
int osp = 0;        //
int odmg = 15;      //initial values

cout <<"Enter 1 to start a turn ";
cin >> input;

if ( input = 1 )
{
turn();
}

while( input != 1) 
{
break;
}

cout << "You're hp: " << myhp << endl;
cout << "You did " << mydmg << " damage." << endl;
cout << "O hp: " << ohp << endl;
cout << "O damage: " << odmg << endl; 

while(myhp<ohp)
{
cout << "You Lose";
getchar();
break;
}

while(ohp<myhp)
{
cout << "You Win"; 
getchar();
break;
} 

while(turns>40)
{
break;
}


cout << "End of Program." << endl;
getchar();

system("pause");
return 0;
}
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?