Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9034569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:30:59+00:00 2026-06-16T08:30:59+00:00

I have this coding down below (just posting the Customers Management only). This a

  • 0

I have this coding down below (just posting the Customers Management only).

This a database in which I am adding a customer each time it passes. Now I need to check whether the c.ID which is the client ID exists or not.

I tried doing a method called searchID which returns 1 if it’s found or -1 if it’s not found.
Problem is when I try to run the program, the program literally hangs there. Whether I press 23 or “ENTER” nothing happens and I would need to exit it using the CTRL + C;

so this is how it works:

When I add a customer (Which is a struct) it saves into the file but I first need to check
whether the ID exists in the database or not otherwise I need to ask the user to either input another ID or go back to the Main Menu

Any suggestions please?? thank you

#include<io.h>
#include<fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "..\Headers\common.h"
#include "..\Headers\customerManagement.h"

static FILE *cfp;
static customer c;
#define STRUCTSIZE sizeof (customer)


/** This is the Customers's Main Menu in which the various sections can be
 *  accessed from here
 */
boolean  customerMainMenu()
{



    int optionC;
    clrscr();

    copyright();


    printf ("\n\n\n\n\t\t    ************* Customer's Main Menu *************\n \n \n");

    printf ("Press [1] to add a new Customer\n");
    printf ("Press [2] to edit a Customer\n");
    printf ("Press [3] to list all Customers\n");
    printf ("Press [4] to Show a Customer's last Order\n");
    printf ("Press [5] to go back to Main Menu\n\n\n");


    if (scanf ("%d",&optionC) == 1)
    {
        switch (optionC)
        {

        case 1:
        {
            clrscr();
            getchar();
            addCustomer();
            break;
        }
        case 2:
        {
            printf ("Edit a Customer\n");
            break;
        }

        case 3:
        {
            clrscr();
            listCustomers();
            getchar();
            while (getchar()!='\n')
            {

            }
            break;
        }
        case 4:
        {
            printf ("Customer's Last Order\n");
            break;
        }
        case 5:
        {
            system ("PAUSE");
            break;
        }
        default:
        {
            if (optionC != 1 || optionC != 2 || optionC != 3 || optionC != 4 || optionC !=5)
            {
                clrscr();
                printf ("Invalid option!\n");
                system ("PAUSE");
                customerMainMenu();
            }
            break;
        }
        }
    }
    return TRUE;

}

/**
 *  This following method will append a customer to the
 *  database at the end of the file
 *
 *  */

void addCustomer ()
{
    char ch;
    copyright();

    printf ("\n\n\n\n\t\t    ************* Add Client **********\n \n \n");

    if ((cfp = fopen ("customers.dat","a+b")) == NULL)
    {
        fputs("Can't open customers.dat file\n",stderr);
    }



    printf ("\tThis will add another customer to the the database\n");
    printf ("\tPress 'Y' to confirm or 'N' to return to the Client Main Menu\n\tWITHOUT adding a customer\n");
    ch = getchar();

    if (ch == 'n' || ch == 'N')
    {
        customerMainMenu();
    }
    else if (ch == 'y' || ch == 'Y')
    {

        clrscr();
        printf ("\n\n\n\n\t\t    ************* Add Client **********\n \n \n");
        printf ("Please enter Name:\n");
        while (scanf ("%s", c.name) == 0 || cCheck(c.name,100) == FALSE);
        {

        }


        printf ("Please Enter Surname: \n");
        while (scanf ("%s",c.surname) == 0 && cCheck (c.surname,100) == FALSE);
        {

        }

        printf ("Please Enter ID Card, [NOTE! Only numbers are allowed!]\n");
        int cID;
        cID = 0;
        while (scanf ("%d",&cID)==0)
        {
            printf ("Only Numbers are allowed!\n");
            while (getchar() != '\n')
            {
            }
        }

        if (searchID(cID) == 1)
        {
            printf ("This ID already exists. Client already exists!\n");
            printf ("Do you want to input another ID or return to Main Menu?\n");
            printf ("Press 'Y' if you enter another ID, press any other key to return to Main Menu\n:");

            ch = getchar();
            if (ch == 'y' || ch == 'Y')
            {
                printf ("Enter another ID:\n");
                while (scanf ("%d",&cID)==0)
                    {
                        printf ("Only Numbers are allowed!\n");
                        while (getchar() != '\n')
                        {
                        }
                    }
                searchID(cID);
            }
            else if (searchID(cID) == -1)
            {
                cID = c.ID;
                getchar();
            }

        }



        while (getchar()!='\n')
        {

        }

        printf ("Please Enter Address:\n");
        gets(c.address);


        fwrite (&c,STRUCTSIZE, 1, cfp);

        printf ("For Testing purposes:\n");
        printf (" %s\n %s\n %s\n %d\n", c.name, c.surname, c.address, c.ID);
        askAnother();


    }
    else
    {
        printf ("\nInvalid choice! Either Y or N is accepted\n");
        system ("PAUSE");
        getchar();
        addCustomer();
    }
}

void listCustomers()
{

    if ((cfp = fopen ("customers.dat","rb")) == NULL)
    {
        fputs("Can't open customers.dat file\n",stderr);
        printf ("Returning to Customer Main Menu");
        system ("PAUSE");
        customerMainMenu();
    }

    rewind (cfp);

    while (fread (&c,STRUCTSIZE,1,cfp)==1)
    {
        printf ("Customer: %s %s ID: %d\n", c.surname, c.name, c.ID);
    }
    fclose (cfp);
    //  system ("PAUSE");

}


void askAnother()
{
    printf ("Do you want to add another Customer?\n");
    printf ("Enter 'Y' for yes and 'N' to return to the Main Menu\n");

    char input;
    input = getchar();

    if (input == 'Y' || input == 'y')
    {
        getchar();
        addCustomer();
    }
    else if (input == 'N'|| input == 'n')
    {

        fclose (cfp);
        customerMainMenu();

    }
    else
    {
        printf ("Invalid Option! Only Y or N are allowed\n");
        system ("PAUSE");
        askAnother();

    }

}


boolean cCheck(char *test, int max)
{
    int x;
    for (x =0; x<max; x++)
    {
        if (isdigit(test[x]))
        {
            return FALSE;
        }
        if (x==max)
        {
            return TRUE;
        }
        x++;

    }
    return TRUE;
}

int fileSize()
{
    int lengthOfFile;
    int file;

    file = open("Customers.dat",O_RDONLY,0);
    lengthOfFile = lseek (file,0, SEEK_END);

    return lengthOfFile;
}

int getNoOfRecords()
{
    return (fileSize()/(STRUCTSIZE));
}

/**
 *  This method will compare the ID passed from the ID of the customer to check
 *  whether it is exists or not. If it exists it will output 1 otherwise it
 *  will output -1. This will make sure that the Person's ID is unique
 *
 */

int searchID (int cID)
{
    // for the while loop
    int index;
    index = 0;
    //gets the number of records currently held in the file.
    int records;
    records = getNoOfRecords();

    //User will input the ID into this variable and it will be checked
    //whether it exists or not


    int IDstatus;
    IDstatus = 0;

    while (index != records)
        {
            fread (&c,STRUCTSIZE,1,cfp);
            if (c.ID == cID)
            {
                IDstatus = 1;
            }
            else
            {
                IDstatus = -1;
            }
        }

    return IDstatus;

}

EDIT:

There are either 2 things:

Either the Method is not working the SearchID() method because even though I have 2 IDs which are 0 now, they are still accepting it

or else because of the c.ID which is staying 0.

When I’m inputting the data, it is accepting it BUT when I try to output the whole record, the Client ID stays 0.

Added to that, it IS letting me having Two IDs which are 0 so most probably the method is not working…. Thanks for all the help until now!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:31:01+00:00Added an answer on June 16, 2026 at 8:31 am

    You missed to increment index, and of course you should exit the loop when you found the id:

    while (index != records)
        {
            fread (&c,STRUCTSIZE,1,cfp);
            if (c.ID == cID)
            {
                IDstatus = 1;
                break; // <<<< otherwise IDStatus will be overwritten by next iteration
            }
            else
            {
                IDstatus = -1;
            }
            index++; // <<< otherwise endless loop
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just a question... I'm seeing this coding style in various php scripts
I have an entity which looks something like this: (I'm coding to the web
Just to lay down some restrictions first. Because of the environment I'm coding this
I have seen lot projects following this kind of coding style Any significant advantage
I am new to Javascript coding and have looked for this information in many
I'm doing a bit of coding, where I have to write this sort of
Im very new to all coding including jquery. I though this would have been
Ive never heard of this before, and I have been coding in PHP for
I have data in this format coming from a database... BUS 101S Business and
ok i am new to coding and cocos2d i have this shooting code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.