Data Entry System

Data Entry System

Hard C Control Flow 30 views
Explanation Complexity

Problem Statement

Input multiple records (name and age) in a loop. After each entry ask "Add more? (y/n)". Validate input - name must not be blank, age must be between 1-120.

Input Format

Multiple records, each containing:

name (string)

age (integer)
After each record, a choice input: y or n.


Output Format

Valid records stored successfully, input stops when user chooses n.

Example

Name: Rahul
Age: 25
Add more? y

Name: Anita
Age: 130
Add more? n
Invalid age for second record, input stopped.

Constraints

• Name must not be blank

• Age must be between 1 and 120

• Choice must be y or n

Concept Explanation

The program repeatedly takes user input and validates each field before accepting it.

Step-by-Step Explanation

1.Start a loop to enter records.

2.Ask the user to enter name.

3.Check if name is empty or blank:

• If yes, show an error and ask again.

4.Ask the user to enter age.

5.Check if age is between 1 and 120:

• If not, show an error and ask again.

6.Store the valid name and age.

7.Ask the user: "Add more? (y/n)".

8.If the input is y:

• Continue the loop.

9.If the input is n:

• Exit the loop.

10.End the program after all valid records are entered.

Concept Explanation

The program repeatedly takes user input and validates each field before accepting it.

Step-by-Step Explanation

1.Start a loop to enter records.

2.Ask the user to enter name.

3.Check if name is empty or blank:

• If yes, show an error and ask again.

4.Ask the user to enter age.

5.Check if age is between 1 and 120:

• If not, show an error and ask again.

6.Store the valid name and age.

7.Ask the user: "Add more? (y/n)".

8.If the input is y:

• Continue the loop.

9.If the input is n:

• Exit the loop.

10.End the program after all valid records are entered.

Input / Output Format

Input Format
Multiple records, each containing:

name (string)

age (integer)
After each record, a choice input: y or n.


Output Format
Valid records stored successfully, input stops when user chooses n.
Constraints
• Name must not be blank

• Age must be between 1 and 120

• Choice must be y or n

Examples

Input:
Name: Rahul Age: 25 Add more? y Name: Anita Age: 130 Add more? n
Output:
Invalid age for second record, input stopped.

Example Solution (Public)

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

int main() {
    char name[50];
    int age;
    char choice;

    while (1) {

        /* ---- Name Input ---- */
        while (1) {
            printf("nEnter name: ");
            fgets(name, sizeof(name), stdin);

            // Remove newline
            name[strcspn(name, "n")] = '�';

            if (strlen(name) == 0) {
                printf("Error: Name cannot be blank.n");
            } else {
                break;
            }
        }

        /* ---- Age Input ---- */
        while (1) {
            printf("Enter age (1-120): ");
            if (scanf("%d", &age) != 1) {
                printf("Error: Invalid age input.n");
                while (getchar() != 'n'); // clear buffer
                continue;
            }

            if (age < 1 || age > 120) {
                printf("Error: Age must be between 1 and 120.n");
            } else {
                break;
            }
        }

        while (getchar() != 'n'); // clear leftover newline

        printf("Record added: %s, %d years oldn", name, age);

        /* ---- Continue? ---- */
        while (1) {
            printf("Add more? (y/n): ");
            scanf(" %c", &choice);

            if (choice == 'y' || choice == 'Y') {
                while (getchar() != 'n');
                break;
            } else if (choice == 'n' || choice == 'N') {
                printf("Input finished.n");
                return 0;
            } else {
                printf("Invalid choice. Enter y or n.n");
            }
        }
    }

    return 0;
}

Official Solution Code

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

int main() {
    char name[50];
    int age;
    char choice;

    while (1) {

        /* ---- Name Input ---- */
        while (1) {
            printf("nEnter name: ");
            fgets(name, sizeof(name), stdin);

            // Remove newline
            name[strcspn(name, "n")] = '�';

            if (strlen(name) == 0) {
                printf("Error: Name cannot be blank.n");
            } else {
                break;
            }
        }

        /* ---- Age Input ---- */
        while (1) {
            printf("Enter age (1-120): ");
            if (scanf("%d", &age) != 1) {
                printf("Error: Invalid age input.n");
                while (getchar() != 'n'); // clear buffer
                continue;
            }

            if (age < 1 || age > 120) {
                printf("Error: Age must be between 1 and 120.n");
            } else {
                break;
            }
        }

        while (getchar() != 'n'); // clear leftover newline

        printf("Record added: %s, %d years oldn", name, age);

        /* ---- Continue? ---- */
        while (1) {
            printf("Add more? (y/n): ");
            scanf(" %c", &choice);

            if (choice == 'y' || choice == 'Y') {
                while (getchar() != 'n');
                break;
            } else if (choice == 'n' || choice == 'N') {
                printf("Input finished.n");
                return 0;
            } else {
                printf("Invalid choice. Enter y or n.n");
            }
        }
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.