# In need of code! [solved]

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

## Recommended Posts

I am trying to make a simple text based "game" using everything I learned up to Ch .4 in this book that I am reading and I have an error with the If Statment. I know that the problem is the "sword == s" line but dont know how to fix it.Im using Dev-C++ and here is my code: #include <iostream> #include <string> #include <ctime> #include <cstdlib> using namespace std; main() { string username; const int MAX_ITEMS = 10; string inventory[MAX_ITEMS]; char sword; int numItems = 0; inventory[++numItems] = "Short Sword"; inventory[++numItems] = "Small Shield"; inventory[++numItems] = "Potion"; inventory[++numItems] = "Potion"; cout << "HI! Welcome to the practice program for Chapter 4!"; cin.ignore(1); cout << "\nMay I have your name? : "; cin >> username; cout << "\nHi " << username << ", here is a list of items im going to start you off with:" << endl; cin.ignore(1); for (int i = 0; i < numItems; ++i) cout << inventory << endl; cin.ignore(1); cout << "\nTo use your Short Sword, just press the key button 's'. Go ahead and try." << endl; cin >> sword; if (sword == s) { cout << "WHOA! WHOA! Stop! Your going to hurt someone!"; else cout << "I said the 's' key on the keyboard, not " << sword << " !." emdl; } system("pause"); return 0; } PS: How do I put my code into that little box as a post since its much easier to read the code like that? [Edited by - Pharaoh12 on March 15, 2006 11:38:38 PM]

##### Share on other sites
I just figured out i have to put a ' before and after the s, but now there is an error on the else statment?

-Pharaoh12

##### Share on other sites
if(sword == 's')	std::cout << "WHOA! WHOA! Stop! Your going to hurt someone!";	else	std::cout << "I said the 's' key on the keyboard, not " << sword << " !." << std::endl;

##### Share on other sites
Quote:
 Original post by Pharaoh12I just figured out i have to put a ' before and after the s, but now there is an error on the else statment?Thanks in advance,-Pharaoh12

1. You've got "emdl" instead of "endl"
2. The else should be outside the if bracket.
Example:
if sword == 's'
{
<STATEMENT>
}
else
{
<STATEMENT>
}

Kay?

##### Share on other sites
1)You type [ source ] text [ /source ] without the spaces between the brackets to get a window with code and you type [ code ] text [ /code] to get text in block lettering
like this

2)change "char sword;" line to "string sword", it's probably closer to what you really want, but that's not what your error probably is.

3)the line "if(sword == s)" probably gives an error that says something like "s is an unidentified identifier" this is because it doesn't know what s is.

what you probably need is

if(sword == "name of sword")

or
if(sword == "s")

or
string s = "exalibur";if(sword == s)

Something like that. The first two just compare the string variable sword with a string literal. The third compares two string variables. They all return true if the the strings are EXACTLY alike. You have to do string manipulation if you want Yes, yes, YES and YeS to all count the same(ie, case insensitive). Hope that is enough info to help.

##### Share on other sites
okay, i figured out the problem, it was that i had to put << before I use endl. But now I want to know how to have the user use "sword" at any time. So anytime the user presses 's', it says the statment that I put in?

##### Share on other sites
Quote:
 Original post by Pharaoh12okay, i figured out the problem, it was that i had to put << before I use endl. But now I want to know how to have the user use "sword" at any time. So anytime the user presses 's', it says the statment that I put in?

If I understand you correctly, you need to use a loop.

##### Share on other sites
Try putting your code into a loop (I suggest a "while" loop) so that unless they press something like Q it loops through the code, with that you can also avoid using a system("pause") call.

##### Share on other sites
yes, but loops end. Isnt there a way that i can have the user use "sword" anytime during the program?

##### Share on other sites
Quote:
 Original post by Pharaoh12okay, i figured out the problem, it was that i had to put << before I use endl. But now I want to know how to have the user use "sword" at any time. So anytime the user presses 's', it says the statment that I put in?

Instead of having a variable as sword, make one called input. And then whenever it detects 's' you can use the sword. Use this in a loop. Example:

[SOURCE]char input;int e = 0;for(;;){cin >> input;switch(input){case 's':  <CODE FOR SWORD USE HERE>  break;case <WHATEVER OTHER INPUT>  break;case 'E': //(for exit)   e = 1 // exiting code   break;} if(e == 1) break;}[/SOURCE]

EDIT: I forgot you can't use exit...it's a system command..heh.

##### Share on other sites
Quote:
 Original post by Pharaoh12yes, but loops end. Isnt there a way that i can have the user use "sword" anytime during the program?

Loops only end when certain conditions are met, if you make those conditions user defined the user can tell the program to end when they want it to.

##### Share on other sites
Thank you "BringBackFuturama", i forgot about the switch statment.And thank everyone else who helped me with this!

-Pharaoh12

##### Share on other sites
Quote:
 Original post by BringBackFuturamafor(;;)

while(!e)
. thats what while's there for!

##### Share on other sites
By using
inventory[++numItems] = "Short Sword";
wont you skip inventory[0], and put the first value into inventory[1]?
I think
inventory[numItems++] = "Short Sword";
would be better.

##### Share on other sites
Quote:
Original post by Cosmic R
Quote:
 Original post by BringBackFuturamafor(;;)

while(!e)
. thats what while's there for!

while(true)
{
}

and for shizzles and giggles while(true); =)

• 11
• 16
• 11
• 11
• 11