Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Problems with editing and showing customer data - c

+1 vote
asked Dec 23, 2021 by Ana Cunha (130 points)
I'm doing a customer and product management project in C. And right now I'm having some problems:

in the part of showing the customer's data, he doesn't show the whole name, only the first letter; the phone number puts a number that I didn't enter; in nif does not say what corresponds; as well as the email that put characters that I did not indicate.
Already in the edit part, it edits but not correctly, because it changes data that I didn't ask for and others I don't sample.

This is my code:

1 Answer

0 votes
answered Dec 28, 2021 by Peter Minarik (55,660 points)
edited Dec 31, 2021 by Peter Minarik

Wrong Data Types Chosen

Without going too deep in your code, let's consider your data structure:

/* List structure declared to store our data. */
typedef struct list {
    int *customer_code;
    char name;
    int age;
    int tin;
    char email;
    double phone;
    char country;
    struct list *next;
} Data;

In C, strings (text, if you prefer) are stored in character arrays (char[] or char *). The type char can only store a single character, hence you need a full array of characters to store strings.

I believe this explains why you face so many problems. Start by replacing your single characters with character arrays.

For simplicity, you can start with fixed array sizes, if it suits your needs. E.g. you make a constraint on the name, that it cannot be longer than, say 50, characters.

For similar reasons, I don't see why your customer_code needs to be a pointer to an integer, instead of a regular int.

The phone number to be a double is probably not the right choice either. I'd store it on a char[MAX_PHONE_LENGTH] or if the size of the structure really matters, on an unsigned long long int (uint64_t, if you include <stdint.h>). The former allows phone numbers to start with + and the country code

A Proposed Fix

Below I propose a fix to your data structure. Of course, you'll need to update your functions that operate on the structure too.

#define MAX_NAME_LENGTH 50

/* List structure declared to store our data. */
typedef struct list {
    int customer_code;
    char name[MAX_NAME_LENGTH];
    int age;
    int tin;
    char email[MAX_EMAIL_LENGTH];
    char phone[MAX_PHONE_LENGTH];
    char country[MAX_COUNTRY_LENGTH];
    struct list * next;
} Data;

Structure Your Code

I've had a bit of free time so I looked at your code.

Normally, you should have a header file to expose your interfaces (public functions, types, declarations, etc); and have a source file that contains the implementation for the exposed functionalities (and other internal -- static -- functions, helpers, etc).

I've restructured your code and did the following implementation: https://onlinegdb.com/HkhC8-dJP


  1. You don't need an "Update" function. Just modify the data pointed by the value returned by the "Find" function
  2. Deleting is a bit tricky because you need to know the left and right sides (previous and next pointers). Using a single linked list you need to do something similar as I did, but using a double linked list you could just reuse the "Find" function as the element to be deleted has a previous and next pointers so you don't need to find them manually.
  3. The main() function only contains some testing and demonstration that the Customer linked list works. Feel free to fix any mistakes I've made.
  4. I didn't want to comment and propose a fix via github, in case you would want to have external reviewers there, so I just share my tips and findings here, on OnlineGDB.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.