This C++ function, `ModificaCampi`, allows the user to update specific...

August 22, 2025 at 11:46 AM

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")) { leggiInput("Nome: ", scheda->Nome, sizeof(scheda->Nome), controllaNome); richiestaValida = true; } else if (strcmp(richiesta, "Cognome")) { leggiInput("Cognome: ", scheda->Cognome, sizeof(scheda->Cognome), controllaNome); richiestaValida = true; } else if (strcmp(richiesta, "Data di Nascita")) { leggiInput("Data di nascita (gg/mm/aaaa): ", scheda->DataNascita, sizeof(scheda->DataNascita), controllaData); richiestaValida = true; } else if (strcmp(richiesta, "Residenza")) { leggiInput("Residenza: ", scheda->Residenza, sizeof(scheda->Residenza)); richiestaValida = true; } else if (strcmp(richiesta, "Sintomi")) { leggiInput("Descrizione sintomi: ", scheda->DescrizioneSintomi, sizeof(scheda->DescrizioneSintomi)); richiestaValida = true; } else { std::cout << "Campo Richiesto non valido.\n"; } } }

This C++ function, ModificaCampi, allows the user to update specific fields of a Paziente structure (likely representing a patient's record). Here's the breakdown of what the code does:

  1. Input Validation Loop:

    • A while loop keeps running until the user inputs a valid field to modify.
    • If the input is invalid, the loop continues after prompting the user and clearing the input buffer.
  2. Field Options:

    • The user is prompted to enter the name of the field they want to update from a list of options: Nome, Cognome, Data di Nascita, Residenza, Sintomi.
  3. Handling User Input:

    • The input is read into the richiesta array using std::cin.getline.
    • If there's an input failure (e.g., input is too long), the buffer is cleared and ignored, and the user is prompted again.
  4. Field Matching and Update:

    • The input in richiesta is compared against the field names using strcmp (note: this comparison appears to be incorrect; will address this below).
    • If the input matches a valid field:
      • The function leggiInput is called to handle the data input and validation for that specific field.
        • For Nome and Cognome, leggiInput validates the name using controllaNome.
        • For Data di Nascita, leggiInput validates the date using controllaData.
        • For Residenza and Sintomi, no additional validation functions are provided.
      • The updated field value is stored in the corresponding variable of the Paziente structure (e.g., scheda->Nome, scheda->Cognome).
      • The richiestaValida flag is set to true, exiting the loop.
  5. Error Handling:

    • If the input does not match any valid field, the user is informed with the message Campo Richiesto non valido. and prompted again.

Notes on Issues:

  1. Incorrect strcmp usage:

    • strcmp(richiesta, "Nome") is expected to return 0 when the strings are equal. The current logic treats non-zero values as a match, which is incorrect.
    • This should be corrected by checking for equality: if (strcmp(richiesta, "Nome") == 0).
  2. Potential Overflow of richiesta:

    • The richiesta array has a fixed size of 20, but no bounds checking is performed on user input length. This can lead to undefined behavior if the input length exceeds 19 characters (plus the null terminator).
  3. Missing Details on leggiInput:

    • The functionality of leggiInput, controllaNome, and controllaData is not provided, so their behavior is unclear. Presumably, they handle input validation and data assignment for specific fields.

Summary:

This function allows the user to modify specified fields of a patient's record (Paziente structure). User input is validated in a loop, and invalid inputs are rejected with appropriate feedback. However, the code has issues that need fixing, especially around string comparison and input handling.

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