undeclared identifier

Hi, i started with a c++ tutorial yesterday and everything was going fine till now.

This is the "program" the compiler cant compile because of 3 undeclared identifier errors:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


#include <iostream> 
using namespace std;

int main()
{
	int array0[7] = {1, 2, 3, 4, 5, 6, 7};
	int array1[7] = {-9, -8, -7, -6, -5, -4, -3};

	int sum[7]; //<-- stores the addition result.
	for(int i = 0; i < 7; ++i)
                        // add the corresponding i-th element and store in sum
			sum[i] = array0[i] + array1[i];
			
			cout << array0[i] << " + " << array1[i] << " = ";
			cout << sum[i] << endl;
}


And these are the errors i get when trying to compile:

c2_16.cpp(16) : error C2065: 'i' : undeclared identifier
c2_16.cpp(16) : error C2065: 'i' : undeclared identifier
c2_16.cpp(17) : error C2065: 'i' : undeclared identifier

Could anyone point me in the right direction or just tell me whats wrong?

TIA!
Last edited on
closed account (z05DSL3A)
In your code the scope of i at line 12 would be that line and the following one. It is therfore out of scope at line 16.

You should use braces to define a block after the line 12, the identifier will be valid of this block.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream> 
using namespace std;

int main()
{
    int array0[7] = {1, 2, 3, 4, 5, 6, 7};
    int array1[7] = {-9, -8, -7, -6, -5, -4, -3};

    int sum[7]; //<-- stores the addition result.
    for(int i = 0; i < 7; ++i)
    {
        // add the corresponding i-th element and store in sum
        sum[i] = array0[i] + array1[i];
			
        cout << array0[i] << " + " << array1[i] << " = ";
        cout << sum[i] << endl;
    }
}
Last edited on
heh, cant believe i missed that, thanks a lot!
Instead of making a new topic, I'll just hijack yours since my problem is similar to yours, and yours is done.

I'm also extremely new and I was messing around with the tutorial source codes that involved arrays and I decided I wanted to add them together, but with a function. Don't bother about the top funtion, that part works. Right now, this is what I have:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int sumarray[3];


void printarray (int arg[], int length)//Simple print fuction
{for (int n=0; n<length; n++) 
	cout << arg[n] << " ";
	cout << "\n";
}

void addition (int array1[], int array2[])//My make-shift addition with arrays function
{for (int i=0; i<4; i++){
int sumarray[3];
sumarray[i] = array1[i] + array2[i];
cout << sumarray[i];}}

int main () {
	int firstarray[] = {3, 4, 5};
	int secondarray[] = {4, 5, 2};
	printarray (firstarray,3);
	printarray (secondarray,3);
	addition (firstarray,secondarray);//works until this line to the addition funtion. 
	return 0;}                                                  


Is it possible to have two arrays being added together and still keep it in a array? Similar to a matrice?

In the console after debugging, I get this:

3 4 5 [top fuction]
4 5 2[top function]
797-1717986920[addition function]


Edit: Also, sorry it is a complete mess. I couldn't find any tutorials on addition of arrays...I was just happy the program would compile. Not sure about this one though because I was trying a bunch of different combos.
Last edited on
1
2
3
4
5
void addition (int array1[], int array2[])//My make-shift addition with arrays function
{for (int i=0; i<4; i++){
int sumarray[3];
sumarray[i] = array1[i] + array2[i];
cout << sumarray[i];}}


should be:

1
2
3
4
5
6
7
8
void addition (int array1[], int array2[]) {
int sumarray[3];

 for (int i=0; i<4; i++) {
  sumarray[i] = array1[i] + array2[i];
  cout << sumarray[i] << " ";
 }
}
Last edited on
Doh! I really appreciate it, I didn't even realize I had the right sum for each column, but I forgot a space XD. I still get -1717986920 though after "7 9 7". Any ideas?
err
 
for (int i=0; i<4; i++) 


shud be:

 
for (int i=0; i<3; i++) 
Topic archived. No new replies allowed.