C (programming language)

C program to display user details using id

Displaying user details based on their ID is a common task in C programs dealing with user data. This guide explores different approaches for achieving this, delving into data structures, file handling, and error handling considerations.

1. Understanding the Task

The program’s objective is to:

  • Receive a user ID from the user as input.
  • Search for the user with the provided ID in a data source.
  • If the user is found, extract and display their relevant details like name, age, email, etc.
  • Handle potential errors like invalid ID or user not found.

2. Approaches and Data Structures

Several approaches can be implemented depending on the complexity and nature of your user data:

2.1. Static Array:

  • Store user data in a statically defined array of structures. Each structure would hold one user’s details (ID, name, age, etc.).
  • Use a loop to iterate through the array and compare IDs until the matching user is found.
  • If found, access and display the corresponding user details from the structure.

2.2. Dynamic Array:

  • Utilize a dynamically allocated array to store user data, allowing for flexibility in handling varying numbers of users.
  • Similar to the static array approach, iterate and compare IDs, displaying details of the matching user.

2.3. File-based Storage:

  • Store user data in a text file, with each line representing a user’s details separated by commas or other delimiters.
  • Open the file and read each line, extracting the ID and comparing it to the user input.
  • If a match is found, parse the line to extract and display the corresponding user details.

2.4. Database Connectivity:

  • For larger datasets, consider interfacing with a database like MySQL or SQLite.
  • Use appropriate SQL queries to search for the user based on the provided ID and retrieve their details.
  • Display the retrieved details to the user.

3. Code Example (Static Array)

Here’s an example C code snippet using a static array:

C

#include <stdio.h>

typedef struct user {
  int id;
  char name[50];
  int age;
  char email[50];
} User;

int main() {
  // Define static array of users
  User users[] = {
    { 1, "John Doe", 30, "[email protected]" },
    { 2, "Jane Doe", 25, "[email protected]" },
    { 3, "Alice Smith", 20, "[email protected]" }
  };
  int num_users = sizeof(users) / sizeof(users[0]);

  // Get user input for ID
  int user_id;
  printf("Enter user ID: ");
  scanf("%d", &user_id);

  // Find user with matching ID
  User* found_user = NULL;
  for (int i = 0; i < num_users; i++) {
    if (users[i].id == user_id) {
      found_user = &users[i];
      break;
    }
  }

  // Display user details or handle not found case
  if (found_user) {
    printf("User found:\n");
    printf("ID: %d\n", found_user->id);
    printf("Name: %s\n", found_user->name);
    printf("Age: %d\n", found_user->age);
    printf("Email: %s\n", found_user->email);
  } else {
    printf("User with ID %d not found.\n", user_id);
  }

  return 0;
}

This code demonstrates searching for a user based on ID in a static array and displaying their details if found. Remember to modify the data structure and search logic based on your chosen approach and data source.

4. Error Handling and Considerations

To enhance your program, consider these points:

  • Input validation: Check for valid user input format (e.g., numeric ID).
  • Error messages: Provide informative messages for scenarios like invalid input or user not found.
  • Memory management: For dynamic arrays, handle memory allocation and deallocation appropriately.
  • File handling: Ensure proper opening, closing, and error checking for file-based approaches.

5. Conclusion

C programs dealing with user data often require searching and displaying details based on specific identifiers like IDs. Understanding different data structures, file handling techniques, and error handling

CodeForHunger

Learn coding the easy way. Find programming guides, examples and solutions with explanations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button