Sign in to follow this  
Pharaoh12

In need of code! [solved]

Recommended Posts

Pharaoh12    100
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[i] << 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 this post


Link to post
Share on other sites
raz0r    586

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


Link to post
Share on other sites
Quote:
Original post by Pharaoh12
I 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 this post


Link to post
Share on other sites
nobodynews    3126
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 this post


Link to post
Share on other sites
Pharaoh12    100
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 this post


Link to post
Share on other sites
raz0r    586
Quote:
Original post by Pharaoh12
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?


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

Share this post


Link to post
Share on other sites
njkt    145
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 this post


Link to post
Share on other sites
Quote:
Original post by Pharaoh12
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?


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


Link to post
Share on other sites
njkt    145
Quote:
Original post by Pharaoh12
yes, 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 this post


Link to post
Share on other sites
andrew_480    163
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 this post


Link to post
Share on other sites
Riekistyx    100
Quote:
Original post by Cosmic R
Quote:
Original post by BringBackFuturama

for(;;)



instead of using that, use
while(!e)
. thats what while's there for!


instead of all that

while(true)
{
}

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

Share this post


Link to post
Share on other sites

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