C++ problems

This topic is 2057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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 Edited by Big T06

Share on other sites
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.

Share on other sites
You should post the error message. Even if it makes no sense to you, chances are there is some useful information in there.

Share on other sites
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

Share on other sites
Could you explain what you think this line does?
 for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) {

Share on other sites
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.

Share on other sites
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.

Share on other sites
Just now looked your code. Can you describe what this line is trying to do?

 for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) { 

Share on other sites
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.

• 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

Share on other sites
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

Share on other sites

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").

That, too, will always resolve to 'true'.

Share on other sites

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

That will simply resolve to 'true' for every execution as his last form would. You are executing attack == "a weapon", which calls std::string's overloaded == operator, and returns true or false. However, it then will execute [true | false] | "weapon". "weapon" is not going to be nullptr, therefor, you are bitwise ORing a [true | false] (generally represented as 0x1 and 0x0) against a guaranteed non-nullptr value, giving you a value that is guaranteed to NOT be 0. You then do it again with "the weapon".

The way that doesn't fail is:

 if (attack == "a weapon" || attack == "weapon" || attack == "the weapon") 

However, judging by the the set of strings, I wouldn't be surprised if one could get away with

 if (attack.find("weapon") != std::string::npos)  Edited by Ameise

Share on other sites

However, it then will execute [true | false] | "weapon". "weapon" is not going to be nullptr, therefor, you are bitwise ORing a [true | false] (generally represented as 0x1 and 0x0) against a guaranteed non-nullptr value, giving you a value that is guaranteed to NOT be 0. You then do it again with "the weapon".
[/quote]
Those are logical OR, not bitwise OR. The compiler determines that "weapon" is a non-null pointer, so you have [some boolean] || true, which evaluates to true, short circuiting the final condition.

However, judging by the the set of strings, I wouldn't be surprised if one could get away with
 if (attack.find("weapon") != std::string::npos) 
[/quote]
To nitpick, the user could type something ambiguous, like: "a magical weapon". In this case the program would behave differently if it checked for the presence of "weapon" or "magic" first.

I think your code behaves reasonably for this, but it is nice to be aware of such edge cases. Edited by rip-off

Share on other sites
-Browser is being stupid, double post.- Edited by Ameise

Share on other sites

However, it then will execute [true | false] | "weapon". "weapon" is not going to be nullptr, therefor, you are bitwise ORing a [true | false] (generally represented as 0x1 and 0x0) against a guaranteed non-nullptr value, giving you a value that is guaranteed to NOT be 0. You then do it again with "the weapon".

Those are logical OR, not bitwise OR. The compiler determines that "weapon" is a non-null pointer, so you have [some boolean] || true, which evaluates to true, short circuiting the final condition.
[/quote]

Whoops. Looked like single-pipes on my screen.

However, judging by the the set of strings, I wouldn't be surprised if one could get away with
 if (attack.find("weapon") != std::string::npos) 

To nitpick, the user could type something ambiguous, like: "a magical weapon". In this case the program would behave differently if it checked for the presence of "weapon" or "magic" first.

I think your code behaves reasonably for this, but it is nice to be aware of such edge cases.
[/quote]

Aye, hence 'get away with'. I'm aware that there is the potential for edge cases, but he would need to handle those separately; there's no clarification in regards to the existence of said edge cases, so I didn't handle them .

Share on other sites
Never mind, I see what the problem is with the way I did that -.-; Edited by RyuunoShinobi

Share on other sites

Just now looked your code. Can you describe what this line is trying to do?

 for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) { 

i know i got that wrong quite badly but all i'm trying to do there is set a for that looks at both of those conditions before going through with the loop.

Share on other sites
Actually don't worry i figured it out i got the game to work i'm cheering, thanks for everyone's input and help