I need help! (Beginner with C++)

Started by
9 comments, last by Matt-D 12 years, 3 months ago
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";
}
}
Advertisement
if (walk == walk) {
is always true.

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

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


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.

Yo dawg, don't even trip.


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


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


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


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='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?


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"").
[/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='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.


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

Yo dawg, don't even trip.


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

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


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

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)

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.

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)

This topic is closed to new replies.

Advertisement