Why cant you compare the value if an int type variables

Why cant you compare the value if an int type variables?

For example:

1
2
3
4
5
6
7
8
9
10
11
12

int a = v.size();

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

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


This is a part of a function here where i and a are both integers. I wrote the condition for if to be that when the value of i is less than the value of a, it should return the value if i otherwise it returns -1.


when i compile this it says:
error: 'i' was not declared in this scope

What am I doing wrong and what can I change or do to be able to make a condition compare the values of two different int type variables?

Many thanks.
Last edited on
The variable i is created in the for loop. It exists only inside the for loop. I have underlined the place where it is created:
1
2
3
4
5
6
7
  for(int i=0; v[i]!=n && i<a; i++){}
if(i<a){
        return i;
    }
    else{
        return -1;
    }


Now, I will underline the entire for loop, so you can see all the places where i exists:
1
2
3
4
5
6
7
  for(int i=0; v[i]!=n && i<a; i++){}
if(i<a){
        return i;
    }
    else{
        return -1;
    }


Do you see the end of the for loop? On the first line.

Everything that follows:
1
2
3
4
5
6
 if(i<a){
        return i;
    }
    else{
        return -1;
    }

is NOT inside the for loop, so the variable i does NOT exist.

That makes so much sense thank you so much.

Last edited on
Does this mean that now values which are attached to functions in the for loop but which are declared outside the for loop also cant have this value used in the if loop ?

for example for the int variable theindex:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int mfind(int n, const std::vector <int>& v){

    int a = v.size();
    int theindex;

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

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

   
}
Last edited on
closed account (E0p9LyTq)
What exactly are you trying to do? Find out what element a specified value is located in?

Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>

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

int main()
{
   std::vector<int> vec = { 0, 9, 125, 6, 14, 8, 22 };

   int index = mfind(14, vec);

   std::cout << "The index was: " << index << '\n';
}

int mfind(int n, const std::vector <int>& v)
{
   for (size_t i = 0; i < v.size(); i++)
   {
      if (v[i] == n)
      {
         return i;
      }
   }

   return -1;
}

The index was: 4
what if the vector vec had more than one number 14 in there? what then would happen?

thank you for the assistance that code makes so much sense
Last edited on
Some people like to call this game "computer science". A famous quote likens that to calling surgery "knife science", but nonetheless it is still possible sometimes to carry out one of the fundamental operations of a science; to whit, an experiment.

what if the vector vec had more than one number 14 in there? what then would happen?

Insert more than one 14, conduct the experiment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>

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

int main()
{
   std::vector<int> vec = { 0, 9, 125, 6, 14, 8, 14 };

   int index = mfind(14, vec);

   std::cout << "The index was: " << index << '\n';
}

int mfind(int n, const std::vector <int>& v)
{
   for (size_t i = 0; i < v.size(); i++)
   {
      if (v[i] == n)
      {
         return i;
      }
   }

   return -1;
}

Last edited on
Repeater is absolutely right, but I'll ask one other thing:

what if the vector vec had more than one number 14 in there? what then would happen?
What would you like to have happen?
Two feasible scenarios in my mind would be (1) returning the first result, or (2) returning a vector of results (or a vector of {index, value} pairs). It's possible to code either one.
Last edited on
Tried now and it only outputs the index of the first 14.

Something confuses me. After the for loops, it should return -1 regardless of whether it found n or not but when I compile it it works as intended?

how is it that it only returns -1 when n is not found?
Last edited on
A function can end only ONCE, because when the function ends, it is not running anymore.

A function ends when it returns.

So when the function reaches return i;, the function stops running.

Tell me, once the function has stopped running because it has reached return i how can it possibly go on to reach return -1?

closed account (E0p9LyTq)
In the example I gave it would find the first and return. The logic of finding a value would have to be changed to find the repeated value..

If you want to search for possible multiple values:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>

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

int main()
{
   std::vector<int> vec = { 14, 9, 125, 6, 14, 8, 22, 14, 14, 12 };

   std::vector<int> index = mfind(14, vec);

   std::cout << index.size() << " indexes found.\n";

   if (index.empty() == false)
   {
      std::cout << "The indexes are: ";

      for (const auto& i : index)
      {
         std::cout << i << ' ';
      }
      std::cout << '\n';
   }
}


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

   for (size_t i = 0; i < v.size(); i++)
   {
      if (v[i] == n)
      {
         found.push_back(i);
      }
   }

   return found;
}

4 indexes found.
They were: 0 4 7 8

Change line 10 to find a value that isn't in the vector, for instance std::vector<int> index = mfind(1, vec);, (recompile) and you get:
0 indexes found.
Last edited on
Are you referring to Repeater's code?
That's how a return statement works. Once return is called, the function ends. The return -1 is never reached if the if-statement (line 19) is entered.
Last edited on
@repeater

That makes much more sense now I was unaware of this thank you.
Topic archived. No new replies allowed.