Step 9: Store the Contact and Increment the Counter

Easy Section 2: Add Contact Module
Solve in Playground →

Problem Statement

Complete the basic Add Contact insertion flow by storing the validated ID, name, and phone number in phonebook[contact_count]. After storing the record, increment contact_count and print Contact Added. Retain every earlier input and validation step exactly as part of add_contact().

Input Format

A menu choice of 1, a valid ID, a name line, a valid phone number, followed by 0 to exit.

Output Format

Print the complete validation workflow followed by Contact Added. The next displayed contact count must reflect the new record.

Constraints

ID must be positive. Name must fit in 49 characters. Phone length must be 1 to 14 characters. A successful insertion increases contact_count by exactly 1.

Sample Input

1
101
Alice Johnson
1234567890
0

Sample Output

Contact Book Ready.
Phonebook Capacity: 100
Contacts: 0
Modules Ready.
Menu:
Add
ID: 101
Valid ID
Name: Alice Johnson
Phone: 1234567890
Valid Phone
Contact Added
Menu:
Exit

Explanation

This task turns the previous input-validation workflow into a real storage operation. The validated record is copied into the next available array element and contact_count is incremented.

Starter Code

#include <stdio.h>
#include <string.h>

struct Contact {
    int id;
    char name[50];
    char phone[15];
};

struct Contact phonebook[100];
int contact_count = 0;

void add_contact();
void display_contacts();
void search_contact();
void update_contact();
void delete_contact();

void add_contact() {
    int id;

    if (scanf("%d", &id) == 1) {
        printf("\nID: %d", id);

        if (id > 0) {
            printf("\nValid ID");

            getchar();

            if (fgets(phonebook[contact_count].name,
                      sizeof(phonebook[contact_count].name), stdin) != NULL) {
                phonebook[contact_count].name[
                    strcspn(phonebook[contact_count].name, "\n")
                ] = '\0';

                printf("\nName: %s", phonebook[contact_count].name);

                {
                    char phone_input[100];

                    if (fgets(phone_input, sizeof(phone_input), stdin) != NULL) {
                        phone_input[
                            strcspn(phone_input, "\n")
                        ] = '\0';

                        printf("\nPhone: %s", phone_input);

                        if (strlen(phone_input) >= 1 &&
                            strlen(phone_input) <= 14) {
                            strcpy(phonebook[contact_count].phone, phone_input);
                            printf("\nValid Phone");

                            // Write your code here
                        } else {
                            printf("\nInvalid Phone");
                        }
                    }
                }
            }
        } else {
            printf("\nInvalid ID");
        }
    }
}

void display_contacts() {
}

void search_contact() {
}

void update_contact() {
}

void delete_contact() {
}

int main() {
    int choice;

    printf("Contact Book Ready.");
    printf("\nPhonebook Capacity: %d", sizeof(phonebook)/sizeof(phonebook[0]));
    printf("\nContacts: %d", contact_count);
    printf("\nModules Ready.");

    while (1) {
        printf("\nMenu:");

        if (scanf("%d", &choice) != 1) {
            break;
        }

        switch (choice) {
            case 1:
                printf("\nAdd");
                add_contact();
                break;

            case 2:
                printf("\nDisplay");
                display_contacts();
                break;

            case 3:
                printf("\nSearch");
                search_contact();
                break;

            case 4:
                printf("\nUpdate");
                update_contact();
                break;

            case 5:
                printf("\nDelete");
                delete_contact();
                break;

            case 0:
                printf("\nExit");
                return 0;

            default:
                printf("\nInvalid Choice");
                break;
        }
    }

    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used