dimanche 30 décembre 2018

Why can't you compare the value of two int type vairables? [duplicate]

This question already has an answer here:

I was just compiling some code while learning c++ and reached this on the compiler.

context: For this code which takes a 1 Dimensional vector v and a value n then tries finding this value n in v. If found returns the index of it and if not it returns -1.

int mfind(int n, const std::vector <int>& v){

int a = v.size();

for(int i=0; v[i]!=n && i<a; i++){
int index = i;
}

if(index < a){
    return index;
}
else{
    return -1;
}

}

Index and i are both integers and when compared in the if condition it outputs an error:

error: 'index' was not declared in this scope.

Is it that that you cannot compare the values of two integers ? if there is another reason to this error I would love to know why and how it is that you can compare the values of two different int type variables. many thanks.

I am new around here if I there is anything wrong with the post or layout etc let me know I will change it. Many thanks.

Aucun commentaire:

Enregistrer un commentaire