• Create Account

Banner advertising on our site currently available from just \$5!

# I need help! (Beginner with 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.

10 replies to this topic

### #1FooBarSteve  Members   -  Reputation: 181

Like
0Likes
Like

Posted 07 January 2012 - 04:02 PM

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";
}
}

### #2megamoscha  Members   -  Reputation: 657

Like
0Likes
Like

Posted 07 January 2012 - 04:08 PM

if (walk == walk) {
is always true.

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

Like
1Likes
Like

Posted 07 January 2012 - 04:17 PM

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";
}
}

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...

#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;
}


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#:

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: ");
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...");
}
}
}


### #4boogyman19946  Members   -  Reputation: 1123

Like
0Likes
Like

Posted 07 January 2012 - 10:49 PM

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...

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?

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.

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#.

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";
}
}

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.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

Like
1Likes
Like

Posted 08 January 2012 - 11:45 AM

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.

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.

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?

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" ("I have two options, one being "walk" The other being "see"").

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.

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#.

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.

### #6boogyman19946  Members   -  Reputation: 1123

Like
0Likes
Like

Posted 08 January 2012 - 12:43 PM

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.

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.

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.

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?

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" ("I have two options, one being "walk" The other being "see"").

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

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.

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#.

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.

Strangely enough, it compiled for me as well, yet I haven't really noticed that either XD
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

### #7Matt-D  Crossbones+   -  Reputation: 1546

Like
0Likes
Like

Posted 08 January 2012 - 05:07 PM

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.

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."

### #8BeerNutts  Crossbones+   -  Reputation: 3396

Like
0Likes
Like

Posted 09 January 2012 - 12:42 PM

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.

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....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."

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.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #9Matt-D  Crossbones+   -  Reputation: 1546

Like
0Likes
Like

Posted 09 January 2012 - 08:48 PM

BeerNuts, I think it belongs especially 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.

### #10BeerNutts  Crossbones+   -  Reputation: 3396

Like
0Likes
Like

Posted 09 January 2012 - 10:38 PM

BeerNuts, I think it belongs especially 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.

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.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #11Matt-D  Crossbones+   -  Reputation: 1546

Like
1Likes
Like

Posted 10 January 2012 - 12:56 PM

BeerNuts, I think it belongs especially 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.

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.

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 always 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.

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