• Create Account

# Switch Topic for C++

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.

4 replies to this topic

### #1Big T06  Members   -  Reputation: 134

Like
0Likes
Like

Posted 16 June 2012 - 05:07 AM

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

### #2Bregma  Crossbones+   -  Reputation: 3513

Like
0Likes
Like

Posted 16 June 2012 - 06:48 AM

Could you post your code? "it doesn't work" is a poor description of a problem.
Stephen M. Webb
Professional Free Software Developer

### #3Big T06  Members   -  Reputation: 134

Like
0Likes
Like

Posted 16 June 2012 - 06:58 AM

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){
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){
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 <img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/sleep.png' class='bbc_emoticon' alt='-_-' />";
}
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, 16 June 2012 - 06:58 AM.

### #4rip-off  Moderators   -  Reputation: 6251

Like
2Likes
Like

Posted 16 June 2012 - 07:33 AM

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, 16 June 2012 - 07:33 AM.

### #5Big T06  Members   -  Reputation: 134

Like
0Likes
Like

Posted 16 June 2012 - 08:01 AM

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

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.

PARTNERS