Jump to content
  • Advertisement

Archived

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

neobie

how to store run-time name into array?

This topic is 5224 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''m making a tic tac toe game... and i want to prompt the user to enter his name and then store in playername[20]. but i can''t get it to work...i''ve tried a lot of diff approaches(ie..gets(), cin.get())...i must been using them wrong...someone help! =\
class TTT_Player
{
	private:
		char player_symbol;
		char playername[20];
	public:
		void MakeMove();
		void SetSymbol();
		void SetName(){ gets(playername);}
		void GetName(){ cout << playername; }		
};

Share this post


Link to post
Share on other sites
Advertisement
My preferred way would be to use the C++ string class. This is in the <string> header, so #include that first. Declare the player name as being of type std::string. You can then read the player name with the std::getline function, which is also in the <string> header.

std::getline(std::cin, playername);

[edited by - Beer Hunter on June 4, 2004 12:54:01 AM]

Share this post


Link to post
Share on other sites
i tried it. But when i try to run it, the program doesn''t allow input and just run straight to the end of program.

same thing happens if i use gets(playername) or cin.get(playername,20) when declared as array..

why?...program only allows input when i use cin >>

Share this post


Link to post
Share on other sites
This sounds unusual. People frequently have similar problems with reading, say, integers from cin — if the user enters something other than an integer, further attemts to read from cin will silently do nothing until a call to cin.clear(). Still, I can''t imagine such a thing happening if you''re only reading strings, so I really don''t know what''s happening here.

Is there any chance that you could try to make a stripped-down program that shows this behaviour, so that we can try to see what''s going on?

Share this post


Link to post
Share on other sites
Not sure why your code wouldn't work. Here's what I quickly put together to test it:


#pragma once
#include <iostream>

class TTT_Player
{
private:
char playername[20];
public:
TTT_Player(void);
~TTT_Player(void);
void SetName() { gets(playername); }
void GetName() { std::cout << playername; }
};

int _tmain(int argc, _TCHAR* argv[])
{
TTT_Player *tp;

tp = new TTT_Player;
tp->SetName();
tp->GetName();
return 0;
}


Worked fine for me.

[edited by - strewth on June 5, 2004 2:54:09 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!