Switch Topic for C++

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

Recommended Posts

Hello I just need some help with getting the switch to read characters instead of numbers, how would i be able to do that because so far when the program compiles the program can't read what i write

Share on other sites
fair enough here you go  // RPG style console game #include <iostream> using namespace std; char e; char c; char item; int healthme; int healthit; int wolf; char attack; char tsw = tsw; char ta = ta; char ts = ts; char itemd; int count = 0; char s = s; char m = m; char magic = magic; char weapon = weapon; int main() { cout << "Now ventur..... no not yet you need experience in the field of battle.\n" "pick up ur weapon because here comes a wolf, what weapons?\n the ones on the ground of course!\n" "quickly now! pick one up! a sword, a axe or the staff? (reply with ts for sword ta for axe and ts for staff)\n"; cin >> item; if(item == tsw) { itemd=75; } else if(item == ta) {itemd = 100; } else if(item == ts) { itemd = 50; } else { itemd == 25; cout << "no weapon??? okay then.\n"; } cout << "now battle!\n\n" ; healthit = 500; while(healthit>150) { cout << "the wolf has " << healthit << " health!"; cout << "now choose a class out of swordsmen (s) or mage (m): "; cin >> c; if(c==s) { healthme = 200; cout << "what do u want to attack with? magic or weapon?"; cin >> attack; if(attack == magic){ cout << "what magic would u like to use, fire, ice or wind? "; cin >> e; switch (e) { case 'wind': cout << "it does weak damage! NOT VERY EFFECTIVE!"; healthit = healthit-25; if(item = ts) healthit = healthit-10; break; case'ice': cout << "it does good damage!"; healthit = healthit-50; if(item = ts) healthit = healthit-10; break; case 'fire': cout << "It looks like it hurts!"; healthit = healthit-75; if(item = ts) healthit = healthit-10; break; } } else if(attack == weapon) { cout << "Where do u want to slash the wolf? the head? or the body? (reply with 'the *****' "; cin >> wolf; switch (wolf){ case 'head': cout << "hits hard but it scratches back! "; healthit = healthit-itemd; healthme = healthme-10; break; case 'body': cout << "the hit makes contact!"; healthit = healthit-(itemd-25); break; } } cout << "health of wolf is now" << healthit << "\n"; cout << "The wolf goes in for a attack! and bites u!"; count++; healthme = healthme - (10*count); cout << "You only have " << healthme << " left!"; cout << "This attack looks like it will hurt more next time!"; } else if(c==m) { healthme = 150; cout << "what do u want to attack with? magic or weapon?"; cin >> attack; if(attack == magic){ cout << "what magic would u like to use, fire, ice or wind? "; cin >> e; switch (e){ case 'wind': cout << "it does weak damage! NOT VERY EFFECTIVE!"; healthit = healthit-35; if(item = ts) healthit = healthit-15; break; case 'ice': cout << "it does good damage!"; healthit = healthit-60; if(item = ts) healthit = healthit-15; break; case 'fire': cout << "It looks like it hurts!"; healthit = healthit-85; if(item = ts) healthit = healthit-15; break; default: cout << "The attack failed! Wrong enchantment (U SPELT IT WRONG DUMBASS!)"; break; } } cout << "health of wolf is now " << healthit << "\n"; if(attack == weapon) { cout << "Where do u want to slash the wolf? the head? or the body? (reply with 'the *****' "; cin >> wolf; switch (wolf){ case 'head': cout << "hits hard but it scratches back! "; healthit = healthit-itemd; healthme = healthme-10; break; case 'body': cout << "the hit makes contact!"; healthit = healthit-(itemd-25); break; default: cout << "MISSED!"; } } cout << "health of wolf is now" << healthit << "\n"; cout << "The wolf goes in for a attack! and bites u!"; count++; healthme = healthme - (10*count); cout << "You only have " << healthme << " left!"; cout << "This attack looks like it will hurt more next time!"; } } cout << "now the wolf looks angry! nows the time to tell you about your special attack!"; if(c == s){ cout << "Imbue your sword with energy of the elements fire, ice or wind. Which do you choose?"; cin >> e; switch (e) { case 'wind': cout << " As the skys above grow dark" << item << "glows brightly, creating a wind that grows stronger with every passing second. \n"; cout << " Then all of a sudden a gust of wind comes and knocks the wolf into the air, as the wind lifts you off the ground above the wolf\n"; cout << " you swing your sword and come crashing back down to earth the the force of a tornado behind ur sword."; healthit = 0; break; case 'ice': cout << " As the air around chills << item << glows brightly, freezing the land around you as" << item << " is filled with the power of 100 ice ages.\n\n" << item << "strikes the wolf and shatters it into a 1000 pieces with a single blow."; healthit = 0; break; case 'fire': cout << " As the sun scorches the land " << item << " glows brightly as fire ignites around you, the heat is drawn towards " << item << "\n"; cout << " as it has the power of the sun. This power is power is then used! The wolf is burnt to a crisp from one slash, and it was hard to see\n"; cout << "if the sword even made contact!"; healthit = 0; break; default : cout << " Oh no the injury's from the fight has made u stagger! (your a horrible speller!)"; } } else if(c == m); { cout << "Imbue your body with the element of fire, ice or wind. Which do you choose?"; cin >> e; switch (e) { case 'wind': cout << "As the skys above grow dark your body fills up with the power of storms, the power flowing through your veins is power enough to create\n"; cout << " tornado's as u use that power to throw the wolf around and into the ground crushing it with the power of ten whirlwinds."; healthit = 0; break; case 'ice' : cout << "As the air chills around you the ground starts to freeze around you, as you concentrate the cold air into the palms of ur hand"; cout << "the cold air is forced towards the wolf as it freezes everything in its path, including the wolf!.\n"; cout << "you walk up to the wolf and with " << item << " shatter the wolf with one swing"; healthit = 0; break; case 'fire': cout << " As the sun scorches the land you start heating up as all the heat from the sun goes into you. its starting to become unbareable\n"; cout << " until you turn all that heat into the power to bellow fire, burning the wolf to crisps as you run out of the fire the wolf is nothing but ash"; healthit = 0; break; default : cout << "oh no!"; if(healthit>0) { cout << "The Wolf is charging! it reaches you and you can't defend yourself from using all your energy from the last attack!\n"; cout << " This looks like the end of you.... already "; } else cout << "Congratulations! you have defeated the wolf! Now for the game to begin!.... maybe later"; } } return 0; } 

i know its a mess but i'm not that good so sorry Edited by Big T06

Share on other sites
You still haven't described the problem. That said, I can see lots of problems in your code. Forget about characters for the moment - your code actually wants to use strings. A character is (more or less) a single letter, digit or symbol. A string is a sequence of zero or more characters. It might be empty, it might be the letter "a", it might be a word, a phrase, a sentence or even an entire book.

C++ provides for "string literals", but these are error prone to use directly. What you should do is use std::string, which is a standard type that provides string functionality. You can convert a string literal into a std::string.

Let us look at what the first part of your program looks like with strings:
 #include <string> // Other includes using namespace std; int main() { cout << "Now ventur..... no not yet you need experience in the field of battle.\n" cout << "pick up ur weapon because here comes a wolf, what weapons?\n the ones on the ground of course!\n" cout << "quickly now! pick one up! a sword, a axe or the staff? (reply with ts for sword ta for axe and ts for staff)\n"; int itemDamage = 25; string item; cin >> item; if(item == "tsw") { itemDamage = 75; } else if(item == "ta") { itemDamage = 100; } else if(item == "ts") { itemDamage = 50; } else { cout << "no weapon??? okay then.\n"; } // ... } 
Note that we declare the variables "just in time", and we initialise all variables (except the string - as a complex class type, default initialisation will create an empty string for us.

Some other points

• Placing more than one letter in single quotes - e.g. 'foo' - is actually an advanced feature of C++ called multi-byte literals. You don't want to use them.

• The following code is actually dangerous:
 char x = x; 
It creates a variable named "x" - and initialises it with itself. Of course, the variable 'x' doesn't have a useful value yet. You cannot depend on the value - even if you test the code and it appears to work, this could be a fluke.

• The following code won't do what you expect:
 if(x = y) { // ... } 
The code is actually an assignment. The value of x is set to the current value of y. Then the value of x is evaluated in a conditional boolean context - in C++ any integral value of "0", or for pointers the null pointer constant, is false. Other values will evaluate as "true". Thus, the effect of the code is:
 x = y; if(x) { // ... } 

What you want is:
 if(x == y) { // ... } 

I see many of these mistakes in your code. A way to avoid this is to add small amounts of functionality, then thoroughly test the new code before you continue writing more code.

When you write lots of untested code, the bugs can interact with each other in very confusing ways, which can end up making your think the bug is somewhere it is not. Edited by rip-off

Share on other sites
thanks for the info, like i said i'm not good at programming, not at all yet, but i'm still learning, what my problem was is that when i try to run the program it wouldn't read my input for picking a weapon then after everytime i try to attack it wouldn't say the same thing over and over again depending on how many letters i used, but ill try to fix that now, thank you

1. 1
Rutin
21
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!