GreatDuke
GreatDuke

Reputation: 214

"i" not available in current context

I am trying a debugging tool for the first time and I came up to the following problem:

When I go step by step through the code with a debugger, and when I step into function ComputeInterest() and put watch on i (mark i and right click on it), watch informs me that

'i' is not available in current context

I'm also curious why watch didn't already put i into local variables. This is my code:

#include <iostream>

double computeInterest (double base_val, double rate, int years)
{
     double final_multiplier;
     for ( int i = 0; i < years; i++ )
     {
          final_multiplier *= (1 + rate);
     }
     return base_val * final_multiplier;
}

int main ()
{
     double base_val;
     double rate;
     int years;
     cout << "Enter a base value: ";
     cin >> base_val;
     cout << "Enter an interest rate: ";
     cin >> rate;
     cout << "Enter the number of years to compound: ";
     cin >> years;
     cout << "After " << years << " you will have " << computeInterest( base_val, rate, years ) << " money" << endl;
}

I'm using the latest version 13.12 of Code:Blocks, g++ (GCC) 4.8.1 and GNU gdb (GDB) 7.6.1.

Upvotes: 2

Views: 2076

Answers (1)

Alex Lop.
Alex Lop.

Reputation: 6875

i is defined and exists only inside that scope (the for loop):

 for ( int i = 0; i < years; i++ )
 {
      final_multiplier *= (1 + rate);
 }

So once you step into one of these lines you will be able to inspect 'i'. And once you are out of this loop, i will be unavailable again.

EDIT:

Make sure that you build with no optimization (-O0) and with debug information (-g) compilation flags.

EDIT2:

It also might be the GDB issue. Because I also worked with GDB v7.5.1, if I remember correctly, and had the same behavior. Maybe on a higher version of GDB you will get the expected behavior. As Baum mit Augen mentioned in the comments below, GDB v7.9 behaves as you expect.

Upvotes: 4

Related Questions