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