In need of code! [solved]

Started by
13 comments, last by Riekistyx 18 years, 1 month ago
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]
Advertisement
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
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;
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?

Originality is dead.
1)You type [ source ] text [ /source ] without the spaces between the brackets to get a window with code and you type text 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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

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?
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.
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.
--------------[Nico Projects]
yes, but loops end. Isnt there a way that i can have the user use "sword" anytime during the program?
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.
Originality is dead.

This topic is closed to new replies.

Advertisement