Archived

This topic is now archived and is closed to further replies.

Getting a players name

This topic is 5046 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 am currrently working on an ascii rpg and have run into a little problem. Right now i need to make it so when you start a new game it asks for your name, which wasnt too hard with my text output function. What i tried doing was initializing the string szpName, having my game putting out on the screen "Enter your name: " in a certain location, then going cin >> szpName. The problem was that the place that the writing was going to was down on the next line, and was getting in the way of everything. Now what i either need is for sombody to tell me how to make the place my writing is going directly after the text, or a completely diffent way. If i wasnt clear enough on what my problem was just tell me and i''ll put up a screenshot.
No left turn unstoned

Share this post


Link to post
Share on other sites
TextOutput("", "Enter your name: ", 0, 22, 18, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, 5);
cin >> szpName;

This is the code



No left turn unstoned

Share this post


Link to post
Share on other sites
Heya,

I am not familiar with the TextOut. However, this is how I would do it in C++, in a standard console program:



string szpName;

cout << "Enter Name: ";
cin >> szpName;

cout << "Hello " << szpName << "!";



The output would be:

Enter Name: Chris
Hello Chris!

This would keep your request for the name and the input on the same line.

Share this post


Link to post
Share on other sites
quote:
Original post by Chryzmo
Heya,

I am not familiar with the TextOut. However, this is how I would do it in C++, in a standard console program:



The TextOut was a funtion i put in myself, so that i could set the location and colour of the text (and a few other things). The problem with the cin is that for some reason it is starting at the next line, i cant really explain why.

Share this post


Link to post
Share on other sites
quote:
Original post by MTT
quote:
Original post by falkone
gotoxy()


Can you explain to me how this works and how to use it?


Oh come on, where''s the fun in that?

Share this post


Link to post
Share on other sites
Here is a screenshot of my problem, i have no idea why it is doing this, it doesnt jump to the next line if i dont do the cin function. I have used cin in other things and it did not make it jump to the next line, can sombody please explain to me how to fix this, i would be very greatful. Oh, and stting the cursors position didnt fix the problem.


Share this post


Link to post
Share on other sites
There isnt one in there, here is what i looks like (took out unneccisary stuff).


HANDLE OutputH;
COORD position = {X, Y};
OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(OutputH, color);
SetConsoleCursorPosition(OutputH, position);

cout << szText;

Share this post


Link to post
Share on other sites
I am by far NO expert, I''m even more of a novice thn you are, but have you tried reseting the curser before using cin? don''t know about the color but may put the input in the right spot.

Technology can either save us or destroy us. Me ... I say it will destroy us if Microsoft is the example.

Share this post


Link to post
Share on other sites
I did try it and thought it was a stroke of genius, i basically used most of those lines from the TextOutput function to put the cursor somwhere. The bottom line is it still didnt work, and i made it put the cursor in the right place (this worked, i didnt get those weird white X''s either), made it pause for a second so i could see it in action, and then when it executed the cin the cursor jumped down and the X''s turnes white again. I think if i dont find a solution soon i will give up on my plan to make it look nice and just have a blank screen when you put in your name.

Share this post


Link to post
Share on other sites
quote:
Original post by Tiffany_Smith
See what result you get using gets() instead of cin.

[edited by - tiffany_smith on February 24, 2004 7:27:48 PM]


cannot convert parameter 1 from 'class
std::basic_string,class
std::allocator >' to 'char *'
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called

I think this means it has to be an array, and szpName a string

[edited by - MTT on February 24, 2004 7:45:05 PM]

Share this post


Link to post
Share on other sites
Why is it outputing all of the white Xes? where is the name "matt" supposed to appear, after the "name :" colon?.

i would suggest that you not use cout or cin. try using a getche routine.

Share this post


Link to post
Share on other sites
quote:
Original post by MTT
quote:
Original post by Tiffany_Smith
See what result you get using gets() instead of cin.

[edited by - tiffany_smith on February 24, 2004 7:27:48 PM]


cannot convert parameter 1 from ''class
std::basic_string,class
std::allocator >'' to ''char *''
No user-defined-conversion operator available that can perform
this conversion, or the operator cannot be called

I think this means it has to be an array, and szpName a string

[edited by - MTT on February 24, 2004 7:45:05 PM]


instead of gets(str); use gets(str.c_str());


Share this post


Link to post
Share on other sites
MTT:

Read the input in like so ...

char szpName[256];

gets(szpName);

Also change the cout in the TextOutput function to puts() and have the TextOutput function take a char instead of the std: stuff.

I'm pretty sure that will work, although I can't test it because my complier keeps crashing on me today.

[edited by - tiffany_smith on February 24, 2004 8:27:18 PM]

Share this post


Link to post
Share on other sites
EvilCrap: The name is supposed to go after the colen, I am using win32 console. And doesnt getche only get 1 char?

Tiffany_Smith: I tried making it an array and using gets(szpName), and it just jumped to the next part of the game before i could write anything.

DerAnged: i tried using gets(str.c_str());, and i couldnt get it to work, if you could show me what i would look like with szpName (i kinda couldnt figure out where to put it) then it might work, i dont know.

Share this post


Link to post
Share on other sites