-2

I am currently working with arrays and moving around the data from array to array, however, that isnt the issue the issue is within my void functions more specifically my for loops i dont know why it won't compile when it looks correct to me and I see in my teacher's note that it is possible to declare an integer in a for loop within a void function but still receiving errors.

Error: exercise11.cpp:49:12: error: ânâ was not declared in this scope for(i=0; n=11; n

Code:

#include <iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <iomanip>
#include <fstream>

using namespace std;

const int MAX = 20;

void reformat(double[], double[], int);
void sum(double[], double[], int);

int main()
{
  double input[MAX];
  double newarray[MAX];
  cout << fixed << right << setprecision(5);
  cout << "Morgan Kiger Lec#1002 Lab#1005 Exercise #11" << endl << endl;

  for (int i=0; i<MAX; i++)
    {
      cin >> input[i];
    }

  reformat(input, newarray, MAX);
  sum(input, newarray, MAX);

  return 0;
}


void reformat(double input[], double newarray[], int MAX)
{ 
  for(int i=0; int n=10; n<MAX; i++, n++)
    {
      newarray[n] = input[i]*2;
    }

  for(i=0; n=11; n<MAX; i++, n++)
    {
      newarray[i] = pow(input[n], 0.3);
    }

  return;
}

void sum(double input[], double newarray[], int MAX)
{
  double sum1;
  double sum2;

  cout << "Input Array" << setw(5) << "2nd Array" << endl;

  for(int i=0; i<MAX; i++)
    {
      sum1 = input[i] + sum1;
    }

  for(int i=0; i<10; i++)
    {
      sum2 = newarray[i] + sum2;
    }

  for(int i=0; i<MAX; i++)
    {
      cout << input[i] << setw(5) << newarray[i] << endl;
    }

  cout << "sum of input valves = " << sum1 << endl;
  cout << "sum of 1st 10 values in changed array = " << sum2 << endl;

 return;
}
Morgan
  • 1
  • 2
  • Punctuation exists for a reason. The error message is telling you everything. There is no "n" declared in the scope of this for-loop; the previous "n" is limited to the for-loop in which it was declared. – MikeC Apr 11 '16 at 21:30

3 Answers3

3

Your syntax is wrong, because only the first statement in a for preamble is the declaration part — you've attempted to use two statements for that job.

However, you can declare multiple variables in a single statement, which is what you presumably intended to do:

for (int i = 0, n = 10; n < NAX; i++, n++)
//   ^^^^^^^^^^^^^^^^^

Now, these variables only exist for the duration of the loop. That is their "scope". For your next loop, you have to declare them again.

So, instead of this:

for(i=0; n=11; n<MAX; i++, n++)

repeat the above, substituting 11 for 10 in the new declaration (if that wasn't a typo).

Also, I believe you wrote input[i] when you meant input[n]. Otherwise n is pointless in the first place.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
0
void reformat(double input[], double newarray[], int MAX)
{ 
for(int i=0; int n=10; n<MAX; i++, n++)
  {
  newarray[n] = input[i]*2;
  }

for(i=0; n=11; n<MAX; i++, n++)
  {
  newarray[i] = pow(input[n], 0.3);
  }

  return;
}

When you declare a variable in a for block as you have done here with the variable i , it only exists for the duration of the following block.

The second for block above needs to declare a new loop variable, since the first i no longer exists, after its block has completed executing.

Arif Burhan
  • 477
  • 3
  • 12
0

You've got couple of problems with the use of variables in the for loops in reformat.

for(int i=0; int n=10; n<MAX; i++, n++)

is not right. A for loop cannot have four clauses. You need to use:

for(int i=0, n=10; n<MAX; i++, n++)

The second problem is that the above declaration makes i and n to be valid only in the for loop. Their scope ends in the for loop. They are not valid in the second for loop. They need to be redeclared in the second for loop.

for(int i=0, n=11; n<MAX; i++, n++)
R Sahu
  • 196,807
  • 13
  • 136
  • 247