Specifically, the 'enter data' function is for the user to enter the data of a person, the case is that when checking that the name is valid, the function tells me that it is valid just before a while to which it enters only if it is invalid, however it still enters.
Here is the code:
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
// tipos definidos
typedef struct persona persona;
// macros
#undef NULL
#define NULL 0
#define NOMBRE_DE_FICHERO "BDD"
#define ESPACIO 32
// prototipos
void menuInicial();
void limpiarCadena(char cadenaAlimpiar[]);
void introducirDatos();
void VerDatos();
void eliminarDatos();
void clrscr();
void gotoxy(int x, int y);
void agregarAfichero(persona *personaNueva);
bool comprobarValidezDeEleccion(char eleccion[], int opcionMayor, int opcionMenor, int cantidadDeCaracteresDeLaOpcion);
bool comprobarEdad(char nombre[]);
bool comprobarNombre(char edad[]);
// cuerpo de funciones y estructuras
struct persona
{
char nombre[50];
char correoElectronico[40];
char direccion[40];
char edad[3];
};
void menuInicial()
{
char eleccion[20];
printf("\n\n\n\t\tHola, que deseas hacer?\n\n\t");
printf("a) Introducir datos de personas\n\t");
printf("b) Ver datos de una persona\n\t");
printf("c) Eliminar datos de una persona\n\t");
printf("d) Salir\n\n\n\t\t\t\t\t\t\t");
printf("Introduce tu eleccion aqui ==> ");
gets(eleccion);
while (comprobarValidezDeEleccion(eleccion, 100, 97, 1) == false)
{
printf("\n\n\n\t\tHola, que deseas hacer?\n\n\t");
printf("a) Introducir datos de personas\n\t");
printf("b) Ver datos de una persona\n\t");
printf("c) Eliminar datos de una persona\n\t");
printf("d) Salir\n\n\n\t\t\t\t\t\t\t");
printf("Has seleccionado una opcion invalida, intentalo de nuevo ==> ");
limpiarCadena(eleccion);
gets(eleccion);
}
switch (eleccion[0])
{
case 97:
introducirDatos();
break;
case 98:
VerDatos();
break;
case 99:
eliminarDatos();
break;
default:
exit(-1);
}
}
bool comprobarValidezDeEleccion(char eleccion[], int opcionMayor, int opcionMenor, int cantidadDeCaracteresDeLaOpcion)
{
int i, longitudDeCadena;
if (strlen(eleccion) == NULL)
{
return false;
}
else
{
if (strlen(eleccion) > NULL)
{
longitudDeCadena = strlen(eleccion);
}
}
for (i = 0; i < longitudDeCadena; i++)
{
if (eleccion[i] == NULL || eleccion[cantidadDeCaracteresDeLaOpcion] > NULL)
{
return false;
}
else
{
if (eleccion[i] < opcionMenor || eleccion[i] > opcionMayor)
{
return false;
}
}
}
return true;
}
void limpiarCadena(char cadenaAlimpiar[])
{
int i;
for (i = 0; i < strlen(cadenaAlimpiar); i++)
{
if (cadenaAlimpiar[i] != NULL)
{
cadenaAlimpiar[i] = NULL;
}
else
{
if (cadenaAlimpiar[i] == NULL)
{
return;
}
}
}
}
void introducirDatos()
{
persona *personaActual;
personaActual = (persona *)malloc(sizeof(persona));
printf("\n\n\t\tIntroduce el nombre de tu usuario ==> ");
gets(personaActual->nombre);
if (comprobarNombre(personaActual->nombre) == true)
{
printf("La cadena es valida");
}
else
{ // este condicional anidado lo hice por desesperacion y se confirmaron mis sospechas
if (comprobarNombre(personaActual->nombre) == false)
{
printf("La cadena no es valida");
}
}
while (comprobarNombre(personaActual->nombre) == false) // la cadena es valida antes de entrar pero no lo es al hacerlo
{
printf("\t\tHas introducido un nombre invalido, intentalo de nuevo ==> ");
limpiarCadena(personaActual->nombre);
fflush(stdin);
gets(personaActual->nombre);
}
printf("\t\tIntroduce la edad de el usuario ==> ");
gets(personaActual->edad);
while (comprobarEdad(personaActual->edad) == false)
{
printf("\t\tHas introducido una edad invalida, intentalo de nuevo ==> ");
limpiarCadena(personaActual->edad);
fflush(stdin);
gets(personaActual->edad);
}
printf("\t\tIntroduce la direccion del usuario ==> ");
gets(personaActual->direccion);
printf("\n\nIntroduce el correo electronico del usuario ==> ");
gets(personaActual->correoElectronico);
agregarAfichero(personaActual);
}
void VerDatos()
{
}
void eliminarDatos() // estas son funciones futuras, no tienen repercusion alguna
{
}
bool comprobarEdad(char edad[])
{
int longitudDeEdad = strlen(edad);
int i;
if (longitudDeEdad > 3 || edad[0] == 32 || edad[0] == 0)
{
return false;
}
for (i = 0; i < longitudDeEdad; i++)
{
if (edad[i] < 48 || edad[i] > 57)
{
return false;
}
}
return true;
}
bool comprobarNombre(char nombre[])
{
int i;
if (nombre[0] == NULL || nombre[0] == ESPACIO || strlen(nombre) < 8)
{
return false;
}
for (i = 0; i < strlen(nombre); i++)
{
if ((nombre[i] >= 97 && nombre[i] <= 122) || (nombre[i] >= 65 && nombre[i] <= 90) || (nombre[i] == 32))
{
}
else
{
if ((nombre[i] > 0 && nombre[i] < 65) || (nombre[i] > 90 && nombre[i] < 97) || (nombre[i] > 122))
{
return false;
}
}
}
}
void agregarAfichero(persona *personaNueva)
{
FILE *fichero;
fichero = fopen(NOMBRE_DE_FICHERO, "a");
fprintf(fichero, "%s\n", personaNueva->nombre);
fprintf(fichero, "%s\n", personaNueva->edad);
fprintf(fichero, "%s\n", personaNueva->direccion);
fprintf(fichero, "%s\n", personaNueva->correoElectronico);
fclose(fichero);
fflush(fichero);
}
I am executing the program from powrshell
specifically the one that it gives me vs code
, that is where I program. On the other hand, note that at some points add a fflush(stdin)
se that is not efficient, I'm desperate. As I already mentioned the program is a simple database.
Welcome to the community!
I'll answer you with a question: Why doesn't your "checkName" function return true?
I see the returns with false, but I don't see a single return true, this could cause execution problems. Try to add it to return "true" until the end, after "surviving" all validations that could throw "false".
Example