C++ problems

Started by
16 comments, last by Tome Steve Srsan 11 years, 9 months ago
Hello everyone i was just wondering what i'm doing wrong with my code, as when i try to compile it comes up with what i describe as code dump, there are a few other problems but once i get the code dump out the way i should be able to fix everything else

#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;
int healthit;
for(healthme=500;healthme<0; ); (healthit=500; 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=="a weapon" , "weapon" , "the weapon") {
cout << "What part of the wolf do you want to attack, the body or the head?";
string bodypart;
cin >> bodypart;
if(bodypart == "the body" , "body") {
healthit=healthit-50;
cout << "You hit the wolf!\n";
}

else if(bodypart == "the head" , "head") {
healthit = healthit-100;
healthme=healthme-20;
cout << "It was a good hit, but it bites back!\n";
}
else cout << "you missed! (spelt it wrong) \n";
}
else if(attack =="magic" , "with magic"){
cout << "what element would you like to attack with? fire lightning, ice, water, or earth?\n";
string magic;
cin << magic;
if(magic == "fire"){
healthit=healthit-50;
cout << "It does good damage!\n";
}
else if(magic == "lightning"){
healthit=healthit-100;
cout << "It does amazing damage!\n";
}

else if(magic == "earth"){
healthit= healthit-70;
cout << "It hits hard!\n";
}
else if(magic=="ice") {
healthit = healthit-30;
cout << "It doesn't seem to hurt much!\n";
}
else if(magic=="water") {
healthit = healthit - 10;
cout << "It seems unaffected!\n";
}
else cout << "You missed! (spelt it wrong!)\n";
}
else cout << "you took to long! (spelt wrong!)";
if(healthit<=100){
cout << "He is Angry now and doing more damage!\n";
healthme = healthme-75;
}
else {
cout << "The wolf goes out to attack!\n";
healthme= healthme-50;
}
}
if(healthme<=0) cout << "Your Dead... Game over i guess?";
else cout << "The wolf is dead! You Win!";

return 0;
}


Sorry about the neatness but would be appreciated if someone could tell me what i'm doing wrong, thank you in advance smile.png
Advertisement
Can you post the compilation error which you describe as code dump, or just the beginning and end of the first error it if it's very large.
You should post the error message. Even if it makes no sense to you, chances are there is some useful information in there.
You seem to have 2 sets of brackets after for, I don't see the point and as far as my C++ knowledge goes you are suppose to have just one.

[color=#000088]for[color=#666600]([color=#000000]healthme[color=#666600]=[color=#006666]500[color=#666600];[color=#000000]healthme[color=#666600]<[color=#006666]0[color=#666600];[color=#000000] [color=#666600]);[color=#000000] [color=#666600]([color=#000000]healthit[color=#666600]=[color=#006666]500[color=#666600];[color=#000000] healthit[color=#666600]<[color=#006666]0[color=#666600];[color=#000000] [color=#666600])[color=#000000] [color=#666600]{

I would suggest a while loop that checks if the players health or the wolfs health is 0 or less instead
Could you explain what you think this line does?
for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) {
You need to describe the problem.

If you are getting a compile time error then you should include the error messages (copy & paste). Note that if there are a lot of messages you should generally ignore all bar the top one or two. Each time the compiler finds some errors, its understanding of the program is going to deviate from your expectations, and these build up into dramatically cascading errors that will be very confusing if you try to understand them.

If the problem is with the code - one trick is to simplify. Don't write too much code at once, write small increments and test them regularly. This will prevent you getting a big build up of errors that generate this "code dump".

If you are getting runtime errors - post the error message, and as much of the program interaction you can.If you are getting strange runtime behaviour, indicate both the expected and desired behaviour. Pasting the output of a sample run of the program might be useful too.
This line also doesn't do what you think it does:
if(attack=="a weapon" , "weapon" , "the weapon") {

I am starting to think that you should start with a working "Hello, world!" program and then slowly build more things into it until it does what you want. Make sure you compile and test your program after writing every few lines. If there is some syntax you are uncertain about (like how to check if a string matches one of several strings), write a little test just for that.

That way you won't end up with a long piece of code full of mistakes: You'll catch the mistakes when you write them and you'll know where they are because most of the code has been tested already and is likely to be correct.
Just now looked your code. Can you describe what this line is trying to do?


for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) {
If you're using visual studio your "code dump" most likely comes from this using

cin << magic; // instead of cin >> magic;


But apart from that your code probably will not run the way you expected it.
Things to help you get started:

  • You need to initialize your variables (especially healthme and helthit)
  • Have a look into && and || operators for if-statements
  • Instead of using a crippled for/loop use a while loop
I am just going to repeat what you have already been told, where you have your if (attack == "a weapon" , should be if (attack == "a weapon" || "weapon" || "the weapon").
Also where you first create healthme and healthit declare the amount there.... int Healthme = 500;

You should really look into the different operator's and method's of calling loops ^_^

This topic is closed to new replies.

Advertisement