keyboard input as string

Posted By: stuted

keyboard input as string - 05/21/12 12:10

Hey

i am mainly a c++ programmer, i need to get used to this language. What i want to ask, what can i use to get input from the user, like i could with std::cin?
I want to put the info into a string.

Code:
typedef struct
{
	int health;
	int armor;
	int attack;
	
	STRING* name;
} CREATURE;



here is my struct. I would like to put the input into the instance of CREATURE called plr. so plr.name.
Posted By: PadMalcom

Re: keyboard input as string - 05/21/12 12:15

Look at the function inkey().
Posted By: stuted

Re: keyboard input as string - 05/21/12 12:26

Okey, i am using it, but how come now, if i use TEXT*

Code:
inkey(plr.name);
	
	TEXT* out_test =
	{
		pos_x = 300;
		pos_y = 250;
		layer = 1;
		font = "Arial#14b";
		string = (plr.name);
		flags = SHOW;		
	}



I get an error that says: plr.name invalid charater?
Posted By: Aku_Aku

Re: keyboard input as string - 05/21/12 16:55

Maybe better if you use inchar instead of inkey.
Read the manual and you will be enlightened.
Posted By: PadMalcom

Re: keyboard input as string - 05/21/12 21:01

Can you post your entire code?
Posted By: stuted

Re: keyboard input as string - 05/21/12 21:16

Here comes the full code:
Code:
//#include <acknex.h>
#include <litec.h>
#include <default.c>

void initalize(void);

typedef struct
{
	int health;
	int armor;
	int attack;
	
	STRING* name;
} CREATURE;

void main()
{
	initalize();
	
}

void initalize(void)
{
	screen_size.x = 800;
	screen_size.y = 600;
	
	CREATURE plr;
	plr.health = 100;
	plr.armor = 10;
	plr.attack = 5;
	
	//FONT* arial_font = "Arial#14b";
	
	TEXT* greetings =
	{
		pos_x = 200;
		pos_y = 150;
		layer = 1;
		font = "Arial#14b";
		string("Welcome to this adventure game! Please enter a name for your new character: ");
		flags = SHOW;		
	}
	
	inkey(plr.name);
	
	TEXT* out_test =
	{
		pos_x = 300;
		pos_y = 250;
		layer = 1;
		font = "Arial#14b";
		string = (plr.name);
		flags = SHOW;		
	}
}



Thank you for your time.
Posted By: 3dgs_snake

Re: keyboard input as string - 05/22/12 06:13

Hi,

I made some changes to your code :

Code:
#include <acknex.h>
#include <default.c>

// Text used to show input
TEXT* input_test =
{
	pos_x = 300;
	pos_y = 250;
	layer = 1;
	font = "Arial#14b";
	strings = 1;
	flags = SHOW;		
}

// Show player name
TEXT *out_test = 
{
	pos_x = 300;
	pos_y = 300;
	font = "Arial#32b";
	strings = 1;
	//flags = SHOW;
}

// Greetings text
TEXT* greetings =
{
	pos_x = 200;
	pos_y = 150;
	layer = 1;
	font = "Arial#14b";
	string("Welcome to this adventure game! Please enter a name for your new character: ");
	flags = SHOW;		
}

void initalize(void);

typedef struct
{
	int health;
	int armor;
	int attack;
	
	STRING* name;
} CREATURE;

void main()
{
	initalize();	
}

void initalize(void)
{
	screen_size.x = 800;
	screen_size.y = 600;
	
	CREATURE plr;
	plr.health = 100;
	plr.armor = 10;
	plr.attack = 5;
	
	// Init string with length
	plr.name = str_create("#255");
	
	// Attach the string to the text
	(input_test->pstring)[0] = plr.name;
	
	// Get player Name
	inkey((plr.name));
	
	/// Print player name into the second panel
	// Hide input panel
	input_test->flags &= ~SHOW;
	
	// Show out panel, and set text color
	out_test->flags |= (SHOW | FILTER | LIGHT);
	out_test->blue = 150;
	out_test->red = 20;
	out_test->green = 160;
	// Copy player name into out_test string
	str_printf((out_test->pstring)[0], "Chracter name : \"%s\"", _chr(plr.name));
}



Best regards.
Posted By: PadMalcom

Re: keyboard input as string - 05/22/12 09:05

To explain it:

TEXT* definitions may only occure outside of functions. If you want to create TEXTs inside a function use the function txt_create.

As you might know as c++ programmer you have to initialize everything that is defined as pointer. So you have to create the plr.name using str_create as 3dgs_snake did.
Posted By: stuted

Re: keyboard input as string - 05/22/12 11:32

Oh thanks for the code and the explain. It takes time to get used to lite-c.
Posted By: PadMalcom

Re: keyboard input as string - 05/22/12 11:47

Yepp, but when you've got it is easy to work with it!
© 2023 lite-C Forums