Sign in to follow this  

C++ problems

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

If you intended to correct an error in the post then please contact us.

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
[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;
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;
}
[/CODE]

Sorry about the neatness but would be appreciated if someone could tell me what i'm doing wrong, thank you in advance [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Big T06

Share this post


Link to post
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 this post


Link to post
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][color=#666600]([/color][color=#000000]healthme[/color][color=#666600]=[/color][color=#006666]500[/color][color=#666600];[/color][color=#000000]healthme[/color][color=#666600]<[/color][color=#006666]0[/color][color=#666600];[/color][color=#000000] [/color][color=#666600]);[/color][color=#000000] [/color][color=#666600]([/color][color=#000000]healthit[/color][color=#666600]=[/color][color=#006666]500[/color][color=#666600];[/color][color=#000000] healthit[/color][color=#666600]<[/color][color=#006666]0[/color][color=#666600];[/color][color=#000000] [/color][color=#666600])[/color][color=#000000] [/color][color=#666600]{[/color]

I would suggest a while loop that checks if the players health or the wolfs health is 0 or less instead

Share this post


Link to post
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 this post


Link to post
Share on other sites
This line also doesn't do what you think it does:
[code] if(attack=="a weapon" , "weapon" , "the weapon") {[/code]

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 this post


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

[code]
for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) {
[/code]

Share this post


Link to post
Share on other sites
If you're using visual studio your "code dump" most likely comes from this using
[CODE]
cin << magic; // instead of cin >> magic;
[/CODE]

But apart from that your code probably will not run the way you expected it.
Things to help you get started:[list]
[*]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
[/list]

Share this post


Link to post
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 this post


Link to post
Share on other sites
[quote name='RyuunoShinobi' timestamp='1340980185' post='4953965']
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").
[/quote]
That, too, will always resolve to 'true'.

Share this post


Link to post
Share on other sites
[quote name='RyuunoShinobi' timestamp='1340980185' post='4953965']
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 [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]
[/quote]

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 [i]nullptr[/i], therefor, you are bitwise ORing a [true | false] (generally represented as 0x1 and 0x0) against a guaranteed non-[i]nullptr[/i] value, giving you a value that is guaranteed to NOT be 0. You then do it again with "the weapon".

The way that [i]doesn't[/i] fail is:

[CODE]
if (attack == "a weapon" || attack == "weapon" || attack == "the weapon")
[/CODE]

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

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

Share this post


Link to post
Share on other sites
[quote]
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.

[quote]
However, judging by the the set of strings, I wouldn't be surprised if one could get away with
[code]
if (attack.find("weapon") != std::string::npos)
[/code]
[/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 this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1340985257' post='4953988']
[quote]
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.
[/quote]

Whoops. Looked like single-pipes on my screen.

[quote name='rip-off' timestamp='1340985257' post='4953988']
[quote]
However, judging by the the set of strings, I wouldn't be surprised if one could get away with
[code]
if (attack.find("weapon") != std::string::npos)
[/code]
[/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.
[/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 this post


Link to post
Share on other sites
[quote name='Codarki' timestamp='1340969899' post='4953927']
Just now looked your code. Can you describe what this line is trying to do?

[code]
for(healthme=500;healthme<0; ); (healthit=500; healthit<0; ) {
[/code]
[/quote]
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 this post


Link to post
Share on other sites
Actually don't worry i figured it out i got the game to work [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] i'm cheering, thanks for everyone's input and help [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

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