Non-zero values

Hi.
I am trying to solve an problem like this:

*/Enter non-zero values in the array*/

#include <iostream>
#define m 3
#define n 3
using namespace std;

int main(int argc, char *argv[]) {
int T[m][n],i,j;
do
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>T[i][j];
}
}
} while(T[m][n]!=0);

return 0;
}

I tried but it doesn't work
When you post code (and we really appreciate seeing code to discuss), please place it inside code tags, like this:

[code]
...your code goes here
[/code]


This preserve your formatting, which makes it much more readable.

Notice that the loop you fashioned to check T is outside the loop where input is obtained. That is only going to test one entry, after all has been entered, and that's not what you need to do.

Further, that "while" loop tests location m, n in the array, but the array is declared only for T[m][n] in size, meaning that m and n are invalid. Arrays are indexed starting at 0, so where m is 3 and n is 3, T[2][2] is the maximum position, and T[3][ anything ] is invalid, as is T[ anything ][ 3 ].

So, you need to fashion the "do while" loop just around cin >> T[i][j] - inside the inerr loop, and it should only test T[i][j], repeating for the correct input (and probably issuing an instruction when required).

I see that you are an early student, and this may be challenging. It is also very typical.
It works! thanks you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 

#include <iostream>
#define m 3
#define n 3
using namespace std;

int main(int argc, char *argv[]) {
	int T[m][n],i,j;
	
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			do
				{
				cout<<" Enter values"<<endl;
				cin>>T[i][j];
				if(T[i][j]==0)
				{
					cout<<" ERROR ["<<T[i][j]<<"] is an invalid value, try again \n";
					cout<<" "<<endl;
				}
			} while(T[i][j]!=0);
		}
	}
	
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* Enter non - zero values in the array */

#include <iostream>

int main()
{
   // use const variables instead of #defines, easier to find errors
   const unsigned num_rows = 3;
   const unsigned num_cols = 3;

   int arr[num_rows][num_cols];

   for (unsigned row_loop = 0; row_loop < num_rows; row_loop++)
   {
      for (unsigned col_loop = 0; col_loop < num_cols; col_loop++)
      {
         do
         {
            std::cout << "arr[" << row_loop << "][" << col_loop << "]: ";
            std::cin >> arr[row_loop][col_loop];
         }
         while (0 == arr[row_loop][col_loop]);
      }
   }
   std::cout << '\n';

   for (unsigned row_loop = 0; row_loop < num_rows; row_loop++)
   {
      for (unsigned col_loop = 0; col_loop < num_cols; col_loop++)
      {
         std::cout << arr[row_loop][col_loop] << ' ';
      }
      std::cout << '\n';
   }
}
arr[0][0]: 1
arr[0][1]: 5
arr[0][2]: 0
arr[0][2]: 0
arr[0][2]: 9
arr[1][0]: 7
arr[1][1]: 0
arr[1][1]: 5
arr[1][2]: 3
arr[2][0]: 0
arr[2][0]: 4
arr[2][1]: 5
arr[2][2]: 6

1 5 9
7 5 3
4 5 6
Hello Tyncho,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Your program with some changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>

constexpr size_t ROW{ 3 }; // <--- These lines can also be put in "main".
constexpr size_t COL{ 3 };

//using namespace std;  // <--- Best not to use.
// http://www.cplusplus.com/forum/beginner/258335/ // <--- Recent post.


int main(/*int argc, char *argv[]*/)  // <--- Not used and not needed.
{
	int T[ROW][COL]{}; // , i, j; // <--- Define "i" and "j" in the for loop. You do not use them outside of the for loop.

	do
	{
		for (int i = 0; i < ROW; i++)
		{
			for (int j = 0; j < COL; j++)
			{
				std::cout << "\n Enter a number: ";
				std::cin >> T[i][j];
			}
		}
	} while (T[ROW][COL] != 0);

	return 0;
}

The #define s work, but it is not the best choice for what you are doing. The example I offer is a better choice.

And for "m" and "n" use a better name. Single letters can be confusing and hard to follow especially in larger programs.

For "main" if you are not going to use "argc" and "argv" in the program there is no need to code it here unless you have a later use for it.

The empty {}s initialize the array to (0) zero which is better than the garbage values that it started out with.

As Niccolo pointed out the do/while loop is a problem. You enter the do /while loop and process the for loops then when you reach the while condition it is always true, so you start over from the beginning with your for loops.

It is always a good idea to precede an input like "std::cin >> ???" with a prompt to let the user know what to enter. I added a simple prompt, but you also do something like std::cout << "\n Enter a number (0 to end): ";. Just a thought.

When I put a comment on the do/while loop the program work as it should. And as Niccolo said the testing in the while condition is in the wrong place and what you are doing will always be true.

You may not be at the point of validating your input, but it is a good idea to check what was entered in the std::cin >> T[i][j]; to make sure it was not a letter and that the number is something that you want.

There are a couple of things I want to code and test before I say something that might be taken the wrong way.

Hope that helps,

Andy
Tx. I did not know it
1
2
3
4
   // use const variables instead of #defines, easier to find errors
   const unsigned num_rows = 3;
   const unsigned num_cols = 3;



So... I have another question:

Why did you use this?
1
2
          std::cout << 
            std::cin >>



And I use
1
2
  				cout<<
				cin>>


What is the difference between them?

A great many follow the guideline that "using namespace std" pollutes the code. This means with all of the many "things" in the std namespace, you now must avoid all collisions in your code.

By not "using namespace std", you are avoiding that problem, but it means you must precede calls to functions and such in the standard namespace with the preceding std::.
Hello Tyncho,


The link I provided is a recent discussion on the subject and is worth looking at.

You can also avoid using using namespace std; with:
1
2
3
4
5
6
7
using std::cin;
using std::cout;
using std::endl;

and later

using std::string;

This way you limit what you are using and not including the entire namespace.

This is most likely ahead of where you are at, but worth reading and again when you are ready for it.
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Hope that helps,

Andy
Topic archived. No new replies allowed.