I have not done any c++ programming yet but was wondering how you use arrays in a for loop for example the code below

int i;
double D_Nath(11),D(11),er(11);
for( i=0;i < 11; i++)
{
D_Nath(i)=PI*D(i)/er(1)*.76554;
}


how dod you get this to work correctly.

Thanks for any help with this.

Dani AI

Generated

A short, practical follow-up to what and pointed out.

The compiler error happened because parentheses after a name do not mean “index here.” In C++ double x(11) creates a single double initialized to 11 (or is parsed like a function form), and x(i) is a function call syntax. If the compiler sees something(i) but something is not callable, you get the MSVC “C2064” style error. Arrays use square brackets and are zero-based, so an array of size 11 holds elements 0..10.

A few concrete tips to avoid related bugs:

  • Use a named constant for the length (e.g., const std::size_t N = 11) and use N everywhere so you do not accidentally go out of bounds.
  • Initialize your storage. Local C-style arrays are uninitialized by default. Prefer std::array or std::vector with an initializer to avoid garbage values.
  • Check for division by zero before dividing by er[i] (or whatever the denominator is).
  • Use std::size_t for indices to avoid signed/unsigned comparisons and enable warnings (GCC/Clang -Wall -Wextra, MSVC /W4) to catch mistakes early.

A modern, safer pattern (not the same code shown earlier) looks like this:

const std::size_t N = 11;
constexpr double PI = 3.141592653589793;
std::vector<double> src(N, 0.0), denom(N, 1.0), out(N, 0.0);

for (std::size_t i = 0; i < N; ++i) {
    if (denom[i] == 0.0) continue; // handle or report
    out[i] = (PI * src[i] / denom[i]) * 0.76554;
}

Extra checks: print index ranges while debugging, use assert(i < N) if unsure, and consider std::transform with a lambda for elementwise operations. These practices make loops clearer and help avoid the kind of index/initialization errors that triggered your original problem.

Recommended Answers

All 4 Replies

Replace the round brackets with square ones [] and you are good to go...Oh and btw it would be a good idea to initialize your arrays.

Replace the round brackets with square ones [] and you are good to go...Oh and btw it would be a good idea to initialize your arrays.

i have tried to change it

int i;
double D_Nath[11],D[11],er[11];
for( i=0;i < 11; i++)
{
D_Nath(i)=PI*D(i)/er(i)*.76554;
}


this is what I have for the code but I am getting a C2064 error in the line with the D_Nath(I)=
any idea what I am doing wrong

i have tried to change it

int i;
double D_Nath[11],D[11],er[11];
for( i=0;i < 11; i++)
{
D_Nath(i)=PI*D(i)/er(i)*.76554;
}

this is what I have for the code but I am getting a C2064 error in the line with the D_Nath(I)=
any idea what I am doing wrong

D_Nath[i]=PI*D[i]/er[i]*.76554;

You forgot to change the () for [] inside statement inside the loop

`D_Nath[i]=PID[i]/er[i].76554;

You forgot to change the () for [] inside statement inside the loop

Thanks for all your help. it worked great!!.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.