Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
not compiled.
where exactly the code problem?

What I have tried:

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
	int a[4][3];
	for (int row = 0; row < 4; row++)
	{
		for (int column = 0; column < 3; column++)
			cin >> a[row][column];
		a[2][column] = 0;
	}
}
Posted
Updated 17-May-21 22:34pm

C++
for (int column = 0; column < 3; column++) // column created here
    cin >> a[row][column];                 // and still exists here
a[2][column] = 0;                          // but no longer in existence here

The variable column is created in the for statement, and so only exists within the scope of the for loop. So on the line a[2][column] = 0; there is no such variable.

In future please include the exact text of any error messages with your question. Especially as compiler errors include the actual line number that it complains about.
 
Share this answer
 
Comments
CPallini 18-May-21 4:27am    
5.
M A.h 2021 18-May-21 5:37am    
Is it clearer to explain?
cin >> a[row][column];
Why this section doesn't run?
Richard MacCutchan 18-May-21 5:43am    
No. Please read my answer again. You really need to learn about the scope of variables i n C++ (and most other languages). To use the column variable when the for loop has terminated, change your code to the following:
int column = 0; // scope will now extend beyond this for loop
for (column = 0; column < 3; column++) // column exists already
    cin >> a[row][column];             // and still exists here
a[2][column] = 0;     // and now, column still exists here.
Quote:
What is the error of this piece of code?

The compiler should have told you something, the error message is here to help you solving problems.
Quote:
What is the error of this piece of code?

As Richard spotted, variables scope is an essential concept that you must understand an master.
Variable Scope in C++ - Tutorialspoint[^]
Scope - cppreference.com[^]
Scope of Variables in C++ - GeeksforGeeks[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900