• Create Account

C++ problems

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.

17 replies to this topic

#1Big T06  Members

145
Like
0Likes
Like

Posted 29 June 2012 - 04:39 AM

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

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;
}
}
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, 29 June 2012 - 04:42 AM.

#2Codarki  Members

462
Like
0Likes
Like

Posted 29 June 2012 - 05:24 AM

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.

#3Álvaro  Members

20268
Like
0Likes
Like

Posted 29 June 2012 - 05:26 AM

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

#4Cryusaki  Members

505
Like
0Likes
Like

Posted 29 June 2012 - 05:26 AM

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.

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

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

#5Álvaro  Members

20268
Like
0Likes
Like

Posted 29 June 2012 - 05:28 AM

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

#6rip-off  Moderators

10730
Like
0Likes
Like

Posted 29 June 2012 - 05:30 AM

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.

#7Álvaro  Members

20268
Like
2Likes
Like

Posted 29 June 2012 - 05:33 AM

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.

#8Codarki  Members

462
Like
0Likes
Like

Posted 29 June 2012 - 05:38 AM

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

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


#9doeme  Members

1185
Like
0Likes
Like

Posted 29 June 2012 - 06:50 AM

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

#10RyuunoShinobi  Members

122
Like
-2Likes
Like

Posted 29 June 2012 - 08:29 AM

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

#11Bregma  Members

8321
Like
0Likes
Like

Posted 29 June 2012 - 08:45 AM

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'.
Stephen M. Webb
Professional Free Software Developer

#12Ameise  Members

1141
Like
0Likes
Like

Posted 29 June 2012 - 09:10 AM

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, 29 June 2012 - 09:10 AM.

#13rip-off  Moderators

10730
Like
1Likes
Like

Posted 29 June 2012 - 09:54 AM

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.

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.

Edited by rip-off, 29 June 2012 - 09:54 AM.

#14Ameise  Members

1141
Like
0Likes
Like

Posted 29 June 2012 - 10:21 AM

-Browser is being stupid, double post.-

Edited by Ameise, 29 June 2012 - 10:23 AM.

#15Ameise  Members

1141
Like
0Likes
Like

Posted 29 June 2012 - 10:23 AM

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.

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.

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 .

#16RyuunoShinobi  Members

122
Like
0Likes
Like

Posted 29 June 2012 - 10:58 AM

Never mind, I see what the problem is with the way I did that -.-;

Edited by RyuunoShinobi, 29 June 2012 - 11:01 AM.

#17Big T06  Members

145
Like
0Likes
Like

Posted 02 July 2012 - 04:36 AM

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.

#18Big T06  Members

145
Like
0Likes
Like

Posted 02 July 2012 - 05:01 AM

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

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.