Jump to content

  • Log In with Google      Sign In   
  • Create Account


C++ not initilizing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Big T06   Members   -  Reputation: 138

Like
0Likes
Like

Posted 05 July 2012 - 09:07 AM

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 Posted Image

Sponsor:

#2 DoctorGlow   Members   -  Reputation: 594

Like
1Likes
Like

Posted 05 July 2012 - 09:20 AM

Look up http://en.wikipedia....ard_declaration (Forward declaration)

#3 Tallkotten   Members   -  Reputation: 288

Like
6Likes
Like

Posted 05 July 2012 - 10:46 AM

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

Edited by Tallkotten, 05 July 2012 - 10:46 AM.


#4 BinaryPhysics   Members   -  Reputation: 294

Like
1Likes
Like

Posted 05 July 2012 - 11:40 AM

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, 05 July 2012 - 11:53 AM.


#5 DavitosanX   Members   -  Reputation: 210

Like
1Likes
Like

Posted 05 July 2012 - 11:59 AM

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.

Edited by DavitosanX, 05 July 2012 - 12:01 PM.


#6 Big T06   Members   -  Reputation: 138

Like
0Likes
Like

Posted 05 July 2012 - 10:48 PM

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

#7 BinaryPhysics   Members   -  Reputation: 294

Like
2Likes
Like

Posted 06 July 2012 - 06:11 AM

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.

Edited by BinaryPhysics, 06 July 2012 - 06:11 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS