if you update a variable in a for loop, how do you access the updated value outside the loop?

Pertinent code segment(I doubt you need all 458 lines :) )

for (k = STARTDIGPOS; k <= shortArrSize + 1; k++)
{
temp = a[k] - b[k] - carry;
if (temp >= 0)
{
c[k] = temp;
carry = 0;
}
else
{
c[k] = temp + 10;
carry = 1;
}
}

for (k = shortArrSize + 2 ; k < longArrSize + 1; k++)
{

temp = a[k] - 0 - carry; //How do I reference the value for carry from the last for loop?
if (temp > 0)
{
c[k] = temp;
carry = 0;
}
else
{
c[k] = temp + 10;
carry = 1;
}
Just as you would access any variable that is in scope. Here's an example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>

int main()
{
  int x = 0;

  for (int i=0; i<5; i++)
    {
      ++x;
      std::cout << "In the loop.... " << x << std::endl;
    }

  std::cout << "Not in the loop..." << x << std::endl;
  return 0; 
}

Topic archived. No new replies allowed.