Integer to Struct

Hi friends, could someone tell me how to convert an integer (num_cliente) to this Struct (cliente)?

int num_cliente = 1234
{
  char mil = '0';
  char cen = '0';
  char dec = '0';
  char uni = '0';
};
struct cliente cliente;

what do you expect to get in mil?

'1' or 1?

and why do you want it at all in this way?

num_cliente = 1234 is a number already.

How should your struct appear if num_cliente has less than four digits, or more than four?

Since the struct is so similar to a character array, I would probably do something like this:

char cliente[5];  //for four decimal digits, maximum
int num_cliente = 1234;
itoa(num_cliente, cliente,10);

Ummmm, this is the easy stuff, what will you do when faced with really difficult problems.

Thanks to everyone for responding.
What I want is to have an integer to store in memory, but in the body of the program I want to use this data as a text string because the data will change from a keyboard, so I want to break it down into digits. I want to use a Struct because that way I can customize the names better since I have several similar Structs.

It’s not the best idea ever…. You would probably be better off having a number and dealing with character inputs…

But to your question, when you have a digit as a character c, then c-'0' evaluates to the integral value

Then it’s just a matter of summing and multiplying by 10,100 and 1000

This is what I'm doing, surely there is a better way to do it

struct cliente
{
  char mil = '0';
  char cen = '0';
  char dec = '0';
  char uni = '0';
};
struct cliente cliente;
num_cliente = eeprom_read_word(0);
cliente_a_txt();
void peso_a_txt()
{
  int respaldo;
  respaldo = num_peso % 10000;
  respaldo = num_peso % 1000;
  peso.cen = (respaldo / 100) + 48;
  respaldo = num_peso % 100;  
  peso.dec = (respaldo / 10) + 48;  
  respaldo = num_peso % 10;  
  peso.uni = (respaldo + 48);
}

Yes, the C-string and related functions like itoa(), atoi(), etc. make those sorts of transformations trivial. See post #3 for one of the simplest examples.

In the C-string the digits are directly accessible as ASCII characters. In post #3, client[0] == '1' or mil. Also, Serial.print(cliente); will produce 1234 on the serial monitor.

Thank you!

But I see that you use matrix not Struct, this does not work with Struct which is what I prefer to use

You are very welcome to choose a programming style that is obscure and laborious to code!

One last question, is it advisable or not to use Struct in these cases?

We are simply discussing different, valid programming styles. The choice is yours.

Ok, thank you!

I think an array of chars is a much more natural representation for the digits of a number, than a struct with names for each digit. Perhaps a compromise with named array indices?

enum digitNames { tenmil, mil, cen, dec, uni };
char digits[6] = "1234";   // room for terminating null
   :
  itoa(5678, digits, 10);
  x = digits[cen] - '0';
1 Like

I find it interesting, thanks!

I would say so. Also if the aim is to use the number in the code for doing math, I would make the number the primary representation of the data in memory and only UI code would deal with characters. Having to maintain coherence between the two is often error prone.

OK JML, I understand, thanks!

BTW, looking at your picture

what kind of LCD and keypad is this ? (seems you share pins ?)

This is a 4x20 LCD and a normal 4x4 matrix keypad, only I use the same pins to control them so I save a little space.