How to pass the entered data of a structure to upper case

2

I want the data entered from the "Books" structure to be directly capitalized. I know we have to use the toupper () function but I do not know how to apply it when I work with structures.

#include<stdio.h>
#include<string.h>
#include<conio.h>


//Estructura del libro.
typedef struct{
    char titulo[100];
    char genero[100];
    int paginas;
    int anio_de_edicion;
    char numero_isbn[100];
    char editorial[100];
}Libros;

//Funcion para datos del libros.
void alta(Libros * libros){
    fflush(stdin);
    printf("Titulo: ");
    gets(libros->titulo);
    printf("genero: ");
    gets(libros->genero);
    printf("paginas: ");
    scanf("%d", &libros->paginas);
    printf("año de edicion: ");
    scanf("%d", &libros->anio_de_edicion);
    fflush(stdin);
    printf("numero isbn: ");
    gets(libros->numero_isbn);
    printf("editorial: ");
    gets(libros->editorial);
 }
    
asked by P. Matias 14.11.2016 в 13:27
source

1 answer

4

I would choose to create a function that would convert any string to uppercase, for example:

#include <ctype.h>

void aMayusculas(char* ptr)
{
  for( ; *ptr, ++ptr )
    *ptr = toupper(*ptr);
}

The second step would be to adapt the reading code to perform the conversions one by one:

void alta(Libros * libros){
  // ...

  aMayusculas(libros->titulo);
  aMayusculas(libros->genero);
  aMayusculas(libros->numero_isbn);
  aMayusculas(libros->editorial_isbn);
}

As an additional note, fflush is designed to be used only and exclusively with exits. fflush(stdin) is not supported by the standard, then it is likely that with other compilers the result is not what you expect. I would instead avoid that use of fflush .

    
answered by 14.11.2016 / 13:36
source