• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Zuhon

I need help! (Beginner with C++)

10 posts in this topic

Hey, I'm a noob with C++, i need some help. I'm trying to make a text RPG, but i have a problem. I have two options, one being "walk" The other being "see", and each one has a different response. But for some reason when i type either one while playing it just says "WinLose" rather than one option being "Win" and the other being "Lose" I have the curly brackets how they should be, but it still says them both. Help!

#include <iostream>
using namespace std;
int main()
{
cout << "What do you want to do?" << endl;
int walk = 1;
int see = 2;
cin >> walk,see;
if(walk==walk){
cout << "Win";
}
if(see==see){
cout << "Lose";
}
}
0

Share this post


Link to post
Share on other sites
if (walk == walk) {
is always true.

You need a separate input variable and then compare variable with walk and see.
0

Share this post


Link to post
Share on other sites
[quote name='Zuhon' timestamp='1325973736' post='4900450']
Hey, I'm a noob with C++, i need some help. I'm trying to make a text RPG, but i have a problem. I have two options, one being "walk" The other being "see", and each one has a different response. But for some reason when i type either one while playing it just says "WinLose" rather than one option being "Win" and the other being "Lose" I have the curly brackets how they should be, but it still says them both. Help!

#include <iostream>
using namespace std;
int main()
{
cout << "What do you want to do?" << endl;
int walk = 1;
int see = 2;
cin >> walk,see;
if(walk==walk){
cout << "Win";
}
if(see==see){
cout << "Lose";
}
}
[/quote]

First you really should not use the using namespace std; line as that gets rid of the point of the std namespace (to avoid naming conflicts).

Your main problem is that walk and see in your code are defined as integer values... walk has a value of 1 and see has a value of 2. Your method of using cin is not correct as you can only store your input in one variable. Also even if you do cin >> walk you will be storing an integer value... walk==walk is always true, as is see==see because you are comparing two variables to itself (1==1 is always true and 2==2 is always true).

In this case you really need to use string variables and compare your inputted values to string literals instead of integer values...

[CODE]
#include <iostream>
#include <string>
#include <algorithm>

int main(int argc, char **argv)
{
//a string variable to hold the choice we want
std::string choice;
//display "Walk or see: " to the console
std::cout<< "Walk or see: ";
//takes input and stores it in choice variable.
std::cin>>choice;
//transforms our string to lowercase.
std::transform(choice.begin(), choice.end(), choice.begin(), ::tolower);
//if we entered walk
if(choice == "walk")
{
//print that we chose walk
std::cout<<"You chose walk!"<<std::endl;
}
//otherwise if we enter see
else if(choice == "see")
{
//print that we chose see
std::cout<<"You chose see!"<<std::endl;
}
else
{
//otherwise print we don't know the command
std::cout<<"What?? I don't understand you!"<<std::endl;
}
//the following is code to wait to close the console until you press a key.
std::cout<<"Press any key to continue..."<<std::endl;
std::cin.ignore();
std::cin.get();
return 0;
}
[/CODE]


A few notes (for others reading this as well):

1. In my opinion the whole "using namespace std;" is evil because you just ruined the entire purpose behind having namespaces!

2. I convert the answer to lowercase to allow for See, see, SEE, and other combinations. In a real game it might be better to use regular expressions.

3. I added an else statement for commands we don't recognize. Really it might be best to not have the commands coded in c++, but using a scripting language..

4. I used the a string class instead of c style character arrays. This means that I was able to check the string without having to do a bunch of C strcmp garbage.

5. Always return 0 in main. Also never use a main that has a void return type. Failure to do this will make your code not compliant with C++ standards.

This thread really is a classic example of why C++ is a terrible language for beginners.... There are so many ways to do things that the compiler will happily accept, but will absolutely not work.

P.S:

Here is some similar code in C#:

[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test2
{
class Program
{
static void Main(string[] args)
{
System.Console.Write("Walk or See: ");
string choice = System.Console.ReadLine();
choice = choice.ToLower();
if (choice == "walk")
{
System.Console.WriteLine("You chose walk!");
}
else if (choice == "see")
{
System.Console.WriteLine("You chose see!");
}
else
{
System.Console.WriteLine("What?? I don't understand you!");
}
System.Console.WriteLine("Press any key to continue...");
System.Console.ReadKey(false);
}
}
}
[/CODE]
1

Share this post


Link to post
Share on other sites
[quote name='shadowisadog' timestamp='1325974644' post='4900455']

First you really should not use the using namespace std; line as that gets rid of the point of the std namespace (to avoid naming conflicts).

In this case you really need to use string variables and compare your inputted values to string literals instead of integer values...
[/quote]

I don't see a problem with using namespace std; for a program such as this. I doubt the OP even knows what namespaces were intended for. His (or her XD) code is nowhere near in position to have naming problems. However, rightfully so, you should probably get in the habit of typing out the namespace (std:: in this case) for the good ol' muscle memory.

Also, why string variables? If you're going to compare with string literals, wouldn't it me more sensible to use enumerators than string literals?

[quote name='shadowisadog' timestamp='1325974644' post='4900455']
This thread really is a classic example of why C++ is a terrible language for beginners.... There are so many ways to do things that the compiler will happily accept, but will absolutely not work.
[/quote]

Well... his code would've worked if it weren't for that one mistake with the if statements, which causes the same problem in C#.


[quote name='Zuhon' timestamp='1325973736' post='4900450']
Hey, I'm a noob with C++, i need some help. I'm trying to make a text RPG, but i have a problem. I have two options, one being "walk" The other being "see", and each one has a different response. But for some reason when i type either one while playing it just says "WinLose" rather than one option being "Win" and the other being "Lose" I have the curly brackets how they should be, but it still says them both. Help!

#include <iostream>
using namespace std;
int main()
{
cout << "What do you want to do?" << endl;
int walk = 1;
int see = 2;
cin >> walk,see;
if(walk==walk){
cout << "Win";
}
if(see==see){
cout << "Lose";
}
}
[/quote]

The operator == takes the left and right hand operands and compares if the left is equal to the other. Here you're asking "Does walk equal walk," which will always evaluate to true. You want to create one variable called, let's say, "input" where you save the input of the player and then you can compare input == walk or input == see which will produce different results.
0

Share this post


Link to post
Share on other sites
[quote name='boogyman19946' timestamp='1325998141' post='4900540']
I don't see a problem with using namespace std; for a program such as this. I doubt the OP even knows what namespaces were intended for. His (or her XD) code is nowhere near in position to have naming problems. However, rightfully so, you should probably get in the habit of typing out the namespace (std:: in this case) for the good ol' muscle memory.
[/quote]

Exactly. You are correct that the OP is not near needing to worry about such things, but not doing so is a bad habit that could cause major headaches in the future.

[quote name='boogyman19946' timestamp='1325998141' post='4900540']
Also, why string variables? If you're going to compare with string literals, wouldn't it me more sensible to use enumerators than string literals?
[/quote]

I use a string variable so I can store the inputted string. If you use an integer you would have to input an integer value. I suppose you could have a menu and use an enumeration, but that is not what the OP was asking to do. To be more precise if we left the original code intact, instead of being able to type "see" or "walk" we would have to enter "1" or "2". Since there is no specification of what "1" or "2" are, it makes sense in the context of the post to assume the OP meant being able to type "see" or "walk" ([b]"I have two options, one being "walk" The other being "see""[/b]).

[quote name='boogyman19946' timestamp='1325998141' post='4900540']
[quote name='shadowisadog' timestamp='1325974644' post='4900455']
This thread really is a classic example of why C++ is a terrible language for beginners.... There are so many ways to do things that the compiler will happily accept, but will absolutely not work.
[/quote]

Well... his code would've worked if it weren't for that one mistake with the if statements, which causes the same problem in C#.

[/quote]

That is not fully correct. Notice that cin is not correct either. I don't know why that compiled, but I highly doubt it does anything good. Also I doubt in C# this behavior would have compiled.
1

Share this post


Link to post
Share on other sites
[quote name='shadowisadog' timestamp='1326044741' post='4900676']
[quote name='boogyman19946' timestamp='1325998141' post='4900540']
I don't see a problem with using namespace std; for a program such as this. I doubt the OP even knows what namespaces were intended for. His (or her XD) code is nowhere near in position to have naming problems. However, rightfully so, you should probably get in the habit of typing out the namespace (std:: in this case) for the good ol' muscle memory.
[/quote]

Exactly. You are correct that the OP is not near needing to worry about such things, but not doing so is a bad habit that could cause major headaches in the future.
[/quote]

I actually find myself being a little annoyed when I look at example code because the example codes are short enough not to have naming conflicts so they tend to use the "using namespace" keyboard and so you're never quite sure which functions are supposed to be in the namespace and which are not.

[quote name='shadowisadog' timestamp='1326044741' post='4900676']
[quote name='boogyman19946' timestamp='1325998141' post='4900540']
Also, why string variables? If you're going to compare with string literals, wouldn't it me more sensible to use enumerators than string literals?
[/quote]

I use a string variable so I can store the inputted string. If you use an integer you would have to input an integer value. I suppose you could have a menu and use an enumeration, but that is not what the OP was asking to do. To be more precise if we left the original code intact, instead of being able to type "see" or "walk" we would have to enter "1" or "2". Since there is no specification of what "1" or "2" are, it makes sense in the context of the post to assume the OP meant being able to type "see" or "walk" ([b]"I have two options, one being "walk" The other being "see""[/b]).
[/quote]

I must say I actually have not thought about that. I was thinking in terms of code rather than user interface XD

[quote name='shadowisadog' timestamp='1326044741' post='4900676']
[quote name='boogyman19946' timestamp='1325998141' post='4900540']
[quote name='shadowisadog' timestamp='1325974644' post='4900455']
This thread really is a classic example of why C++ is a terrible language for beginners.... There are so many ways to do things that the compiler will happily accept, but will absolutely not work.
[/quote]

Well... his code would've worked if it weren't for that one mistake with the if statements, which causes the same problem in C#.

[/quote]

That is not fully correct. Notice that cin is not correct either. I don't know why that compiled, but I highly doubt it does anything good. Also I doubt in C# this behavior would have compiled.
[/quote]

Strangely enough, it compiled for me as well, yet I haven't really noticed that either XD
0

Share this post


Link to post
Share on other sites
[quote name='boogyman19946' timestamp='1325998141' post='4900540']
The operator == takes the left and right hand operands and compares if the left is equal to the other. Here you're asking "Does walk equal walk," which will always evaluate to true. You want to create one variable called, let's say, "input" where you save the input of the player and then you can compare input == walk or input == see which will produce different results.
[/quote]

Strictly speaking, it's not generally ("generally" as in "for all types") true -- e.g., for floating-point numbers there's actually this handy way of testing whether a value is NaN:
http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.15
Relying on this property: "If your compiler produces a NaN, it has the unusual property that it is not equal to any value, including itself. For example, if a is NaN, then
a == a is false."
0

Share this post


Link to post
Share on other sites
[quote name='Matt-D' timestamp='1326064074' post='4900775']
[quote name='boogyman19946' timestamp='1325998141' post='4900540']
The operator == takes the left and right hand operands and compares if the left is equal to the other. Here you're asking "Does walk equal walk," which will always evaluate to true. You want to create one variable called, let's say, "input" where you save the input of the player and then you can compare input == walk or input == see which will produce different results.
[/quote]

Strictly speaking, it's not generally ("generally" as in "for all types") true -- e.g., for floating-point numbers there's actually this handy way of testing whether a value is NaN:
[url="http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.15"]http://www.parashift....html#faq-29.15[/url]
Relying on this property: "If your compiler produces a NaN, it has the unusual property that it is not equal to any value, including itself. For example, if a is NaN, then
a == a is false."
[/quote]

What's the point of this response, in a Beginner's forum, in a topic where the OP is obviously a new programmer. The most you've done is confuse the OP, and try to show how smart you are.

IMO, these types of post do not belong in a thread like this.
0

Share this post


Link to post
Share on other sites
BeerNuts, I think it belongs [i]especially [/i]in a Beginners' Forum -- note that in the C++ FAQ I've linked to it's located in the "newbie section" -- and it's no accident. IMHO it's better to learn about the quirks of floating-point numbers (if not the details, then at least the existence thereof) early on, as soon as one learns about the existence of "float" and "double" types, rather than to discover them by accident and waste hours (we're talking about a hypothetical "new programmer" here after all) on debugging the code that made the assumption that they behave like, say, integers. It's also better not to give anyone the mistaken impression of how the operator== works on such an important and fundamental (esp. for game development) family of types.
0

Share this post


Link to post
Share on other sites
[quote name='Matt-D' timestamp='1326163694' post='4901164']
BeerNuts, I think it belongs [i]especially [/i]in a Beginners' Forum -- note that in the C++ FAQ I've linked to it's located in the "newbie section" -- and it's no accident. IMHO it's better to learn about the quirks of floating-point numbers (if not the details, then at least the existence thereof) early on, as soon as one learns about the existence of "float" and "double" types, rather than to discover them by accident and waste hours (we're talking about a hypothetical "new programmer" here after all) on debugging the code that made the assumption that they behave like, say, integers. It's also better not to give anyone the mistaken impression of how the operator== works on such an important and fundamental (esp. for game development) family of types.
[/quote]

It's like if you were teaching 3rd grade math, and, in explaining triangles, you start describing unit circles, and the relationship of the angles and how they match up with sine, cosine, and tangent. Sure, it might be true, but all you've succeeded in doing is confuse them even more, and probably frustrate them.
0

Share this post


Link to post
Share on other sites
[quote name='BeerNutts' timestamp='1326170330' post='4901180']
[quote name='Matt-D' timestamp='1326163694' post='4901164']
BeerNuts, I think it belongs [i]especially [/i]in a Beginners' Forum -- note that in the C++ FAQ I've linked to it's located in the "newbie section" -- and it's no accident. IMHO it's better to learn about the quirks of floating-point numbers (if not the details, then at least the existence thereof) early on, as soon as one learns about the existence of "float" and "double" types, rather than to discover them by accident and waste hours (we're talking about a hypothetical "new programmer" here after all) on debugging the code that made the assumption that they behave like, say, integers. It's also better not to give anyone the mistaken impression of how the operator== works on such an important and fundamental (esp. for game development) family of types.
[/quote]

It's like if you were teaching 3rd grade math, and, in explaining triangles, you start describing unit circles, and the relationship of the angles and how they match up with sine, cosine, and tangent. Sure, it might be true, but all you've succeeded in doing is confuse them even more, and probably frustrate them.
[/quote]

Well, I disagree. It's already categorized under the "newbie section" in C++ FAQ and I happen to agree with this categorization. It's really programming 101, irrespectively of the language -- and if you don't learn it soon enough, it will bite you in the a** in the most unexpected moment. I also take issue with this sloppy attitude of "let's "simplify" stuff to the point of teaching bad habits, we can always correct that later, it would only be confusing to teach this now" -- in my opinion, if anything, it's better simply not to make deceptively "intuitive" claims like "walk==walk is [b]always[/b] true" at all -- at least that would be a genuine simplification that avoids introducing bad habits. Failing that, it's better to correct the claims early on. But to persist in insisting on the incorrect explanation in the mistaken belief that it somehow makes things more intuitive or easier for a beginner is just wrong and ends up being ultimately more confusing (potentially resulting in hours of frustration spent on correcting subtle bugs that might result from such an attitude).

While simpler is usually better when introducing a topic, wrong is rarely simpler or better.
1

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  
Followers 0