### I want to check for duplicate.....can you help me please?

The par that is bold is about checking for double, I have tried any thing I could but it doesn't work...please help me on this. ( Please let me know if the hole code is needed)

void cis_32(){
int midterm_grade,midterm,final_exam;
static int studnet_ID;
static int a[10][4];
bool valid=false;
static int i;
i++;
do{
printf("\n\n\tPlease Enter the student ID\n");
cin>>student_ID;
if (!( student_ID >=1 && student_ID <= 100)) {
cout<<" Please inter a number between 1 and 100";
valid=false;
}
else
valid=true;

}while(valid==false);
if (i>1){

do{

for (int counter=0;counter<=i-1; counter++){
if (a[i-1][0]==student_ID ){
valid=false;
cout<<"Duplicate id , try again";
}
else
valid =true;
}
}while(valid==false);
Last edited on
 ``12345678`` ``````for (int counter=0;counter<=i-1; counter++) { if (a[i-1][0]==student_ID ) { valid=false; cout<<"Duplicate id , try again"; } else valid =true; }``````

As you iterate through the for loop, which occurance of a[] are you referencing?
Aren't you referencing the same one every time?

PLEASE USE CODE TAGS (the <> formatting button). It makes it easier to help you.
i would be counted as the effective size of my array and it will check if there is any given a[i-1][0]=student_ID, in other words, I'm searching trough my a[i-1][0]

I hope I have answered your question.....
and after checking the a[i-1][0] for a duplicate I will set it equal to the student id
You missed my point. I understand i is the number of elements in your array.

Lets assume you have entered 19 students, therefore i is 19.
The if statement inside the for loop becomes:
 `` `` ``if (a[18][0]==student_ID )``

The whole point of the for loop is to check each element of the array. You're checking the same element each time through the loop.

What you want is this:
 `` `` ``if (a[counter][0]==student_ID )``

This way, you're checking a different element of the array each time through the loop.

Topic archived. No new replies allowed.