This code implements the registration and logging of two types...
This code implements the registration and logging of two types of data: Cliente
(Customer) and Ordine
(Order), by using struct
, union
, and enum
. Here's a detailed breakdown of its functionality:
Overall Logic
- Purpose:
- The program allows the user to input data for either a
Cliente
or anOrdine
and logs this information to both the console and an external file (log.txt
).
- The program allows the user to input data for either a
- Key Concepts:
enum TipoStruct
: Identifies whether the data being processed is related to aCliente
or anOrdine
.union
withinRegistro
: Stores either aCliente
or anOrdine
as part of a larger structure, depending on the type indicated by the enum.
Step-by-step Functionality
1. Data Structures
-
Cliente
Structure: Represents customer data including:nome
,cognome
,email
: Strings representing the customer's first name, last name, and email.puntiFedelta
: Integer representing loyalty points.
-
Ordine
Structure: Represents order data including:prodotto
: Product name.codiceid
: Unique identifier for the product.quantita
: Quantity of the product ordered.prezzo
: Double value representing the price of the order.
-
Registro
Structure: Keeps track of:tipo
: An enum type field (StructCliente
orStructOrdine
) indicating the type of data.data
(aunion
): Can hold either aCliente
orOrdine
record.
2. User Input
-
The program begins by prompting the user to choose whether they want to log Cliente (customer) or Ordine (order) data using a
fgets
input. -
If the user selects
Cliente
:- Prompts for
nome
,cognome
,email
, andpuntiFedelta
to define theCliente
structure inside theRegistro
.
- Prompts for
-
If the user selects
Ordine
:- Prompts for
prodotto
,codiceid
,quantita
, andprezzo
to define theOrdine
structure inside theRegistro
.
- Prompts for
-
If the input is invalid:
- The program terminates by outputting "Scelta non valida" (Invalid choice).
3. Logging via gestisciLog
-
The function
gestisciLog
takes a pointer to theRegistro
structure and determines how to process it based on thetipo
field:- For
StructCliente
:- Logs details of the
Cliente
(name, last name, email, and loyalty points) to both the console andlog.txt
.
- Logs details of the
- For
StructOrdine
:- Logs details of the
Ordine
(product, ID, quantity, and price) to both the console andlog.txt
.
- Logs details of the
- For
-
The function uses
fprintf
to append the details to the file (log.txt
), opening it in append mode. If the file cannot be opened, it outputs an error message usingperror
.
4. Closing and Output
- The program closes the
log.txt
file after writing and exits successfully when the logging operation is complete.
Example Interaction
Scenario 1: Logging a Cliente
- User Input:
Vuoi aggiungere: Cliente ordine Ordine? Cliente Nome: Mario Cognome: Rossi Email: mario.rossi@email.com Punti Fedelta: 100
- Console Output:
LOG Cliente: Mario Rossi (mario.rossi@email.com) - Punti: 100
- Contents of
log.txt
:LOG Cliente: Mario Rossi (mario.rossi@email.com) - Punti: 100
Scenario 2: Logging an Ordine
- User Input:
Vuoi aggiungere: Cliente ordine Ordine? Ordine Prodotto: Laptop Codice ID: 12345 Quantita: 2 Prezzo: 999.99
- Console Output:
LOG Ordine: Laptop C-ID:12345 Quantita: 2 - Prezzo: 999.99
- Contents of
log.txt
:LOG Ordine: Laptop C-ID:12345 Quantita: 2 - Prezzo: 999.99
Key Points
- Union and Enum Usage: The
union
inRegistro
allows efficient storage of either aCliente
orOrdine
without allocating memory for both, with theTipoStruct
enum guiding which data to access. - Input Handling: The program sanitizes input by removing newlines using
strcspn
and ensures valid conversion for numeric fields. - Logging: The design separates the input gathering and logging logic, ensuring modularity via the
gestisciLog
function.
This approach is efficient for simple applications requiring selective logging of different types of structured data.