Sign in to follow this  
Tome Steve Srsan

C++ not initilizing

Recommended Posts

i only have one problem and i have one problem why its not working, and i can't figure out what is wrong
[CODE]
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello everyone and welcome to this game.\n";
cout << "In this game the aim is to attack the wolf with what you think is the best attack, goodluck!\n";
int healthme=500;
int healthit=500;
while(healthme>0 && healthit>0 ) {
cout << "Your Health is " << healthme << " and the wolfs health is " << healthit << "\n";
cout << "So, what attack do you want to hit the wolf with a weapon or with magic?";
string attack;
cin >> attack;

if (attack.find("weapon") != std::string::npos) {
cout << "What part of the wolf do you want to attack, the body or the head?\n";
string bodypart;
cin >> bodypart;
if(bodypart.find("body") != std::string::npos) {
healthit-= getdamage(50, 10);
cout << "You hit the wolf!\n";
}

else if(bodypart.find("head") != std::string::npos) {
healthit -= getdamage(100, 20);
healthme -= getdamage(20, 5);
cout << "It was a good hit, but it bites back!\n";
}

else cout << "you missed! (spelt it wrong) \n";
}
else if (attack.find("magic") != std::string::npos){
cout << "what element would you like to attack with? fire lightning, ice, water, or earth?\n";
string magic;
cin >> magic;
if(magic == "fire"){
healthit-= getdamage(50, 10);
cout << "It does good damage!\n";
}
else if(magic == "lightning"){
healthit-= getdamage(100, 10);
cout << "It does amazing damage!\n";
}

else if(magic == "earth"){
healthit-= getdamage(70, 10);
cout << "It hits hard!\n";
}
else if(magic=="ice") {
healthit -= getdamage(30, 5);
cout << "It doesn't seem to hurt much!\n";
}
else if(magic=="water") {
healthit -= getdamage(10, 2);
cout << "It seems unaffected!\n";
}
else cout << "You missed! (spelt it wrong!)\n";
}
else cout << "you took to long! (spelt wrong!)";
if(healthit>=0){
if(healthit<=100){

cout << "He is Angry now and doing more damage!\n";
healthme -= getdamage(75, 10);
}
else {
cout << "The wolf goes out to attack!\n";
healthme-= getdamage(50, 10);
}
}
}
if(healthme<=0) cout << "Your Dead... Game over i guess?";
else cout << "The wolf is dead! You Win!";

return 0;
}
// Returns a random value within DamageRange of Damagebase
int getdamage(int damagebase, int damagerange)
{
// if DamageBase is 50, and DamageRange is 10, DamageBase will be 40 here
int damagedealt = damagebase - damagerange;
// if DamageRange is 10, then this will add anywhere from 0 to 20 damage*
damagedealt += (rand() % (damagerange*2));
return damagedealt;
}


[/CODE]

when compiling the program the error
'getdamage' identifier not found
i can't figure out whats wrong its probably something stupid but still thanks for any help anyway [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites
Everything in C++ must be declared before it is used or the compiler has no idea about what it's trying to use. Function declarations are simple because they just the function header (the header must be identical to the header in the definition).

The brilliant this about declarations is that you could move your function to a different source file and, as long as the function is declared, you are free to use it as much as you like.[source lang="cpp"]# include <stdlib.h>

int getdamage(int, int);

/* entry point */
int main(int argc, char *argv[])
{
/* function body */

return 0;
}

/* return damage */
int getdamage(int damagebase, int damagerange)
{
return damagebase - damagerange += rand() % (damagerange * 2); /* we need to seed rand with a call to srand */
}
[/source]Also remember that identifiers for arguments in declarations don't necessarily need to be the same (or even exist at all).

You never call to change the seed for the random number function either. With that code you'll get the same effects over and over...

Anddd if you use the standard namespace at the beginning of the source file you don't need to then state the the string type your using is from it. There's no harm in it (in fact it's better to avoid using declarations completely for whole namespaces) it's just redundant. Edited by BinaryPhysics

Share this post


Link to post
Share on other sites
Just as a side note, you would benefit greatly by learning about classes. Try the tutorials at cplusplus.com: [url="http://www.cplusplus.com/doc/tutorial/"]http://www.cplusplus.com/doc/tutorial/[/url] , I learned a lot about C++ there.

Also, I believe the right term for your problem is "the following code won't compile". I don't think there's such thing as C++ initialization. Edited by DavitosanX

Share this post


Link to post
Share on other sites
sorry but thats what my problem so i named it as such but thank you anyway [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] yea i'm still new to c++ obviously so thanks for all the help so fair [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

Share this post


Link to post
Share on other sites
[quote name='Big T06' timestamp='1341550094' post='4956205']sorry but thats what my problem so i named it as such[/quote]

This is a compile-time error. It'd be a good habit to get into call errors with their proper name. Initialisation is the issue of a object not having it's data properly set up and is a run-time error. Edited by BinaryPhysics

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