0

I've started programming using C++ few weeks back. I'm working on an application store user input data into an array list. When entering user data the application must be able to check whether the user already exists in the array list. The program is unable to store the user input or able to check whether the user already exists in the array list..

int linearSearch(int array[], int size, int searchValue)
{
    for (int i = 0; i < size; i++)
    {
        if (searchValue == array[i])
        {
            return i;
            break;
        }
    }

    return -1;
}



void customerReg(){

    const int Capacity = 99;
    int cnic[Capacity];
    int customerNic;                     
    int search = 0;

    cout << "Enter Customer NIC: \n";
    cin >> cnic[Capacity];


    search = linearSearch(cnic, Capacity, customerNic);

    if (search != -1){
        cout << "Customer is already registered!\n";
    }

    else {
        string customerName;
        cout << "Enter Customer Name: \n";
        cin >> customerName;

    }
3
  • What is your question, then? Are you looking for a more efficient way to do it? Is it not working? Commented Jan 7, 2016 at 21:02
  • You didn't initialize your array cnic. So, when you first search it, you may find the value or not, as the values are undefined (could be anything). Commented Jan 7, 2016 at 21:05
  • 1
    You should have a variable keeps track of how many {valid} integers are in the cnic array. I recommend using std::vector and not using arrays. Commented Jan 7, 2016 at 21:07

1 Answer 1

2

What about:

...
cout << "Enter Customer NIC: \n";
cin >> customerNic;    // <=== instead of: cnic[Capacity];

Other remarks:

  • the break is not necessary: the return will already interupt the search loop
  • cnic[Capacity] is out of range, so puting a value in it might cause some troubles
  • cnic[] is not initialised
  • It is not clear how you fill cnic[], which is by the way local to the function and be lost as soon as you return from it.
  • Depending how you initalize/fill cnic, it could make sense to keep track of the number of customers that are registered in the table.

Edit:

I assume that you can't use vectors or maps for your exercise, and that you're right at the beginning of your learning.

So I suppose that customerReg() is the first function that you are working on, and that others will follow (display, delete, modifiy...). If this is the case, you have to keep your customer data outside the functions:

const int Capacity = 99;
int cnic[Capacity] {}; 
int customer_count=0;    // counter to the last customer inserted

Then in customerReg() you should call your search function using the number of customers instead of the maximal Capacity:

search = linearSearch(cnic, customer_count, customerNic);

Later, in the else branch you have to insert the new id into the array:

else {
    if (customer_count==Capacity) {
       cout << "Oops ! Reached max capacity"<<endl; 
    else { 
       string customerName;
       cout << "Enter Customer Name: \n";
       cin >> customerName;
       ... 
       cnic[customer_count] = customerNic; // here you store the id
       ... // store (I don't know where) the other customer elements you've asked for 
       customer_count++;  // increment the number of users that are stored. 
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

And the array content would be ...?
@Ilya yes, I was editing when you let your comment. I suppose this is a code extract because nobody can be wondered not to find anything in an empty table.
OK, but even if it's a code extract it's wrong because cnic is local to the function. The whole storage logic is missing, it's not just some errors.
@Ilya yes, and OP doesn't store anything in it. I suppose that he put it there just for the sake of the MCVE. OP needs to give us more of his code if we have to address array fillong as well.
@Christophe If I use customerNic to get user input, How do I make the customerNic input to store in the cnic[] array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.