#How do I Get a number from user in safe way?

22 messages · Page 1 of 1 (latest)

dawn tartan
#

hello, I wanted to ask how to get a number from user in C within range and will ask again if value is not in range and if in range continue!

typedef struct {
    uint8_t IpV4_1;//unsigned in 0-255 (8 bit int)
    uint8_t IpV4_2;
    uint8_t IpV4_3;
    uint8_t IpV4_4;
    uint8_t IpV4_SubNetMask;
    
}IPFormater;
IPFormater IPformater1;

 printf("Enter the target IP address: format X.X.X.X\\X ,\\ = SubNetMask(0-32) Enter 0 in subnet mask to get the defult IP address you entered ");
 scanf("%hhu.%hhu.%hhu.%hhu\\%hhu", &IPformater1.IpV4_1, &IPformater1.IpV4_2, &IPformater1.IpV4_3, &IPformater1.IpV4_4, &IPformater1.IpV4_SubNetMask);

I don't want values higher then 255 and lower then 0

robust fractalBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

chilly cypress
#
int i = NOT_IN_RANGE
while (i > MAX || I < MIN){
   scanf into i 
}
dawn tartan
#

WHAT IS NOT_IN_RANGE?

chilly cypress
#

you start with a value that is not in range
so you enter the loop
then you update the value
-> if its in range you exit the loop
-> if not you scan the value again

dawn tartan
#

Let me chack one second second

#

how do I use this?

chilly cypress
dawn tartan
#

stoppp

#

I really need help did you mean like

int i = 256
while (i > 255 || I < 0){
   scanf into i 
}

?
what is scanf into i?

dawn tartan
chilly cypress
#

did you ever use scanf

dawn tartan
#

yes but I don't know what is this into i and what is I?

#

did you mean?
int i = 256;
while (i > 255 || i < 0) {
scanf("%d", &i);
}?

dawn tartan
dawn tartan
#

unsinged so if you enter bigger number it just take the % I think

#

you can check it yourself

#

so I need help more

dawn tartan
#

!cloce

#

!close