This program is a simple implementation of a system to...

August 22, 2025 at 11:47 AM

#include <iostream> #include <cstring> #include <cctype> #define MaxBuffer 100 #define MaxSintomi 255 class Paziente { public: char Nome[MaxBuffer]; char Cognome[MaxBuffer]; char DataNascita[MaxBuffer]; char Residenza[MaxBuffer]; char DescrizioneSintomi[MaxSintomi]; void StampaCampi() const { std::cout << "\n--- Dati Paziente ---\n"; std::cout << "Nome: " << Nome << "\n"; std::cout << "Cognome: " << Cognome << "\n"; std::cout << "Data di nascita: " << DataNascita << "\n"; std::cout << "Residenza: " << Residenza << "\n"; std::cout << "Descrizione sintomi: " << DescrizioneSintomi << "\n"; std::cout << "---------------------\n"; } }; // Controlla se il nome o cognome contengono solo lettere bool controllaNome(const char* input) { for (size_t i = 0; i < strlen(input); ++i) if (!std::isalpha(static_cast<unsigned char>(input[i]))) return false; return true; } // Controlla se la data contiene solo numeri o '/' bool controllaData(const char* input) { for (size_t i = 0; i < strlen(input); ++i) if (!(std::isdigit(static_cast<unsigned char>(input[i])) || input[i] == '/')) return false; return true; } // Metodo di lettura di input in modo sicuro void leggiInput(const char* campoRichiesto, char* buffer, size_t dim, bool(*controllo)(const char*) = nullptr) { bool valido = false; // Continuo a richiedere il input se non e valido while (!valido) { // Stampo il campo richiesto e leggo il input dal utente std::cout << campoRichiesto; std::cin.getline(buffer, dim); // COntrollo se il input e vuoto if (strlen(buffer) == 0) { std::cout << "Il campo non può essere vuoto.\n"; continue; } // Controllo se i input sono validi if (controllo && !controllo(buffer)) { std::cout << "Caratteri non validi per il campo.\n"; continue; } // Se l'input supera il buffer scarta il resto if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(10000, '\n'); std::cout << "Input troppo lungo, sono stati salvati solo i primi " << (dim - 1) << " caratteri.\n"; } valido = true; } } void ModificaCampi(Paziente *scheda){ char richiesta[20]; bool richiestaValida = false; while(!richeistaValida){ std::cout << "Inserisci il campo da aggiornare: \nNome\nCognome\nData di Nascita\nResidenza\nSintomi\n"; std::cin.getline(richiesta, 20); if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(1000, '\n'); std::cout << "Campo Richiesto non valido.\n"; continue; } if (strcmp(richiesta, "Nome") == 0) { leggiInput("Nome: ", scheda->Nome, sizeof(scheda->Nome), controllaNome); richiestaValida = true; } else if (strcmp(richiesta, "Cognome")== 0) { leggiInput("Cognome: ", scheda->Cognome, sizeof(scheda->Cognome), controllaNome); richiestaValida = true; } else if (strcmp(richiesta, "Data di Nascita")== 0) { leggiInput("Data di nascita (gg/mm/aaaa): ", scheda->DataNascita, sizeof(scheda->DataNascita), controllaData); richiestaValida = true; } else if (strcmp(richiesta, "Residenza")== 0) { leggiInput("Residenza: ", scheda->Residenza, sizeof(scheda->Residenza)); richiestaValida = true; } else if (strcmp(richiesta, "Sintomi")== 0) { leggiInput("Descrizione sintomi: ", scheda->DescrizioneSintomi, sizeof(scheda->DescrizioneSintomi)); richiestaValida = true; } else { std::cout << "Campo Richiesto non valido.\n"; } } } int main() { Paziente paziente; // Ciamo il metodo per input e memorizzazione sicura std::cout << "Inserisci dati Paziente!\n"; leggiInput("Nome: ", paziente.Nome, sizeof(paziente.Nome), controllaNome); leggiInput("Cognome: ", paziente.Cognome, sizeof(paziente.Cognome), controllaNome); leggiInput("Data di nascita (gg/mm/aaaa): ", paziente.DataNascita, sizeof(paziente.DataNascita), controllaData); leggiInput("Residenza: ", paziente.Residenza, sizeof(paziente.Residenza)); leggiInput("Descrizione sintomi: ", paziente.DescrizioneSintomi, sizeof(paziente.DescrizioneSintomi)); paziente.StampaCampi(); // Chiede se chiamare il campo di aggiornamento dati std::cout << "\nVuoi aggiornare un dato. Si/No?\n"; char risposta[2]; std::cin>>risposta; if(strcmp(risposta, "Si")!=0 || strcmp(risposta, "si")!=0 ||strcmp(risposta, "S")!=0 || strcmp(risposta, "s")!=0){ ModificaCampi(&paziente); } else { std::cout << "Nessuna richiesta di aggiornamento.\n"; } return 0; }

This program is a simple implementation of a system to manage patient information. It allows a user to input and update details about a patient using the Paziente class. Below is a detailed breakdown of what the code does:


Key Functionalities:

  1. The Paziente Class:

    • This class represents a patient's data and contains fields:
      • Nome (Name)
      • Cognome (Surname)
      • DataNascita (Date of Birth)
      • Residenza (Residence/Address)
      • DescrizioneSintomi (Symptom Description)
    • A method StampaCampi() is included to display the patient's details neatly.
  2. Validation Helper Functions:

    • controllaNome: Checks if a given string (e.g., Nome, Cognome) contains only alphabetic characters. Returns true if valid, otherwise false.
    • controllaData: Ensures that a date string contains only digits or slashes (/). Designed to validate input formatted as gg/mm/aaaa (e.g., 01/01/2000).
  3. Input Reading Function – leggiInput:

    • Prompts the user for input for a specific field (e.g., name, birth date).
    • Optionally validates the input using a validation function (e.g., controllaNome for name fields, controllaData for dates).
    • Handles issues such as:
      • Empty input.
      • Input exceeding the buffer size (predefined limits: MaxBuffer and MaxSintomi).
    • Safeguards against invalid input, re-prompting the user until valid input is provided.
  4. Field Modification – ModificaCampi:

    • Asks the user which field of the patient record they want to update (choices: Nome, Cognome, Data di Nascita, Residenza, or Sintomi).
    • Validates and updates the corresponding field with new input.
  5. Main Functionality:

    • Creates a Patient Record:
      • Prompts the user to fill in the patient's details: name, surname, date of birth, residence, and symptoms.
      • Uses the leggiInput function for safe and validated input.
    • Displays the Patient Record:
      • Calls the StampaCampi() method to print the patient's details to the console.
    • Provides Option to Update Fields:
      • Asks the user if they want to modify any patient data.
      • If the user agrees, it calls the ModificaCampi function to allow updates.

Code Behavior and Workflow in main:

  1. User is prompted to input patient details (all fields are validated, if applicable).
  2. The patient's details are printed to confirm the input.
  3. The user is asked if they want to modify any of the fields. If the answer is yes, they are allowed to choose a field and update it.
  4. The program exits after processing or confirming the user's input and updates.

Issues and Possible Improvements:

  1. Logic Issue in main for the Update Prompt:

    • The line checking if the user wants to update (if(strcmp(risposta, ...)!=0)) will fail due to improper handling of multiple possible answers (Si, si, etc.).
    • The correct comparison is missing logical conjunctions (&&) or parentheses for clarity.
  2. Potential Buffer Overflow:

    • Using std::cin.getline and relying on sizeof does not fully safeguard against buffer overflows in limited buffers like richiesta[20]. Consider introducing further bounds checks or using std::string.
  3. Hard-Coding of User Inputs:

    • The input fields (Nome, Cognome, ...) are handled via hard-coded strcmp operations. Using an enumeration or a map of field names could simplify this process.
  4. User Guidance:

    • The program could provide clearer error messages (e.g., if input validation fails repeatedly, show examples of valid input).
  5. Default Behavior on Invalid Choices:

    • In ModificaCampi, if the user enters an unrecognized field name, it outputs an error and re-prompts them. This is correct but could frustrate users without providing additional clarification.

Example Workflow:

  1. Input Phase:

    Inserisci dati Paziente!
    Nome: John
    Cognome: Doe
    Data di nascita (gg/mm/aaaa): 15/05/1990
    Residenza: New York
    Descrizione sintomi: Cough and fever
    
  2. Display Phase:

    --- Dati Paziente ---
    Nome: John
    Cognome: Doe
    Data di nascita: 15/05/1990
    Residenza: New York
    Descrizione sintomi: Cough and fever
    ---------------------
    
  3. Update Phase:

    Vuoi aggiornare un dato. Si/No?
    Si
    Inserisci il campo da aggiornare: 
    Nome
    Nome: Jane
    
  4. Output After Update:

    --- Dati Paziente ---
    Nome: Jane
    Cognome: Doe
    Data di nascita: 15/05/1990
    Residenza: New York
    Descrizione sintomi: Cough and fever
    ---------------------
    

Summary:

The code defines a patient class, provides a way to input, validate, display, and modify patient data, and handles user interaction. While functional, it has minor issues requiring bug fixes and improvements for robustness and user experience.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node