Sign in to follow this  

Inputting more than one Character

This topic is 3297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to create a text-based RPG to get myself more familiar, and I am trying to get to be able to have the user input his name, but I don't know how to input more than one character. If someone can tell me how, I'd much appreciate it. Also...as a question on the side, seeing as how I'm pretty new to C++, I've always wondered - what's the use/importance of a pointer? I know it points to the address and not the value, but I don't see the use...Again, I'd appreciate it if someone told me =).

Share this post


Link to post
Share on other sites
Quote:
I've been trying to create a text-based RPG to get myself more familiar, and I am trying to get to be able to have the user input his name, but I don't know how to input more than one character. If someone can tell me how, I'd much appreciate it.

You say that you're using C++ so you can use The C++ String Class.

Quoting from the link I provided:
Quote:
String I/O is easy, as strings are supported by cin.

cin>>my_string;

Quote:
what's the use/importance of a pointer? I know it points to the address and not the value, but I don't see the use

I am probably not the best person to answer about pointers, but I can say I've found them useful when trying to incorporate polymorphism and virtual member functions into my code. Edit: I guess there's also passing arguments by reference. Pointers are used in implementing different data structures. Another type of pointer is a smart pointer. Explore the Boost Library for more information about their implementation of smart pointers.

Again, I'm not an expert so I will defer that topic to the more expert members here. My advice is to just research pointers more and you will know when you need to use them as you grow as a programmer.

Share this post


Link to post
Share on other sites
Thanks for the fast reply, but I tried the strings with cin, and I get an error when I try to build it.

int main()
{
std::string name;
cout << "What is your name?\n";
cout << ">";
cin >> name;

return 0;
}

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

The link you gave me (and your post) said that cin was supports strings, so I have no clue why this is happening...can you tell me what's wrong?

Share this post


Link to post
Share on other sites
did you include <string>? are you specifying 'using namespace std'?

I can't begin to tell you all about pointers but maybe I can clarify things a little for you. Basically a pointer is also a value, an integer is a value representing an integer value, a pointer in turn is a value representing an address in memory. On a 32 bit machine this is essentially the same as an integer since a memory address is just a number. By saying its a pointer you are independent of whether the machine is 32 bit, 64 bit etc or perhaps the machine could even have a wholly different way of addressing memory.
When you create a value by declaring a variable say X, for example an integer then it is placed in memory at an address, say Addr(X). A simple example of one of the many uses of pointers would be that you have a function and you want this function to change the value of X. You pass the address, i.e. a pointer to X as a parameter. In the function you now know where X is located in memory and you can use that information to change X. If you were to just pass in X by value, then X would be copied and there is no way to alter the original X.
There are tons of places where pointers are useful (references are often an alternative, or in higher level languages objects are generally not copied by value by default). With a pointer you have the address of the variable in memory its a tool which can be used all over.

Its very late, I hope Im making any sense.

Share this post


Link to post
Share on other sites
Quote:
Original post by Lordimm
Thanks for the fast reply, but I tried the strings with cin, and I get an error when I try to build it.

int main()
{
std::string name;
cout << "What is your name?\n";
cout << ">";
cin >> name;

return 0;
}

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

The link you gave me (and your post) said that cin was supports strings, so I have no clue why this is happening...can you tell me what's wrong?


try:


int main()
{
std::string name;
std::cout << "What is your name?\n";
std::cout << ">";
std::cin >> name;

return 0;
}


or add using namespace std;

like:


using namespace std;
int main()
{
string name;
cout << "What is your name?\n";
cout << ">";
cin >> name;

return 0;
}


You just have to make sure yr namespaces are adhering to the proper C++ syntax.

Share this post


Link to post
Share on other sites
Oh, sorry for the confusion, yes I am using namespace std, here's the full code

#include <iostream>
using namespace std;

int main()
{
std::string name;
cout << "What is your name?\n";
cout << ">";
cin >> name;

return 0;
}

Again, sorry for the confusion, and thanks Cprogrammer, that helped me a lot =).

Share this post


Link to post
Share on other sites

This topic is 3297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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