i just finished a project for school and im now polishing some simple things i ignored until the program worked fine. Im almost done but I realized I dont know how to limit the amount of chars i can accept on input. For ex: i have this fragment of code:
printf("\nIngrese su intento: ");
scanf("%s", &intento);
The project is a wordle clone so it ALWAYS should be 5 chars, not 1more or 1less, how do i limit it to only accept 5 and in case user inputs lets say 8, tell the user "no, do it again". Googling i found this:
Anything or nothing. Welcome to undefined behavior. Writing beyond the end of an array / buffer causes undefined behavior. What happens when undefined behavior happens is not predictable. Maybe it works correctly. Maybe your program crashes. Maybe you corrupt data and cause a security problem. Maybe your hard-drive gets formatted.
Ok. Now i know that even though it sometimes work, i shouldnt do it cause in some random case, it might not... how do i fix this? how do i limit the input with chars? i've already done this with int because its easier but i dont know how to approach it with text.
printf("Bienvenido a Wordle, cuantas partidas deseas jugar en tu sesion de juego? ");
scanf("%d", &cantPartidas);
while (cantPartidas > 8 || cantPartidas < 1) {
printf("\nLo sentimos, el numero de partidas por sesion tiene que estar entre 1 y 8 :( \nIngresa un numero dentro del rango: ");
scanf("%d", &cantPartidas);
}
printf("\nGenial! Empecemos.");