C++ not initilizing

Started by
5 comments, last by Alex Melbourne 11 years, 9 months ago
i only have one problem and i have one problem why its not working, and i can't figure out what is wrong

#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;
}




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 smile.png
Advertisement
Look up http://en.wikipedia....ard_declaration

([color=#000000][font=sans-serif]Forward declaration)[/font]

That which DoctorGlow said. You need to declare it before the main function otherwise main wont know there is such a function. Similar of how you handle a .h file when you got several classes (if you got any experience with that).

I'd guess that by simply typing
int getdamage(int damagebase, int damagerange);


before
int main()
and after
using namespace std;
the code would work
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.
Just as a side note, you would benefit greatly by learning about classes. Try the tutorials at cplusplus.com: http://www.cplusplus.com/doc/tutorial/ , 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.

Starting out in game programming? Me too! Check out my blog, written by a newbie, for the newbies. http://myowngamejourney.blogspot.mx/

Also, if you don't mind a 5 seconds ad, here's a great free ebook for beginners on the subject of pygame: http://bit.ly/19Bem2q

sorry but thats what my problem so i named it as such but thank you anyway smile.png yea i'm still new to c++ obviously so thanks for all the help so fair biggrin.png
sorry but thats what my problem so i named it as such


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.

This topic is closed to new replies.

Advertisement