You are explicitly asking the user for the dimensions, so what is the problem? You have to assume either row-major or column-major (i.e. for:
1 2
3 4
row major ordering would be (1,2,3,4) where column major would be (1,3,2,4) ), but once you specify how you expect the input there is no reason to need line break characters, etc.
Also, please please please change variable names like 'd', and 'e' to "numberOfRows", etc.
David
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
It looks like you are reading in all of the values, then only writing out the matrix once. What you have looks good, but I would suggest making it into a function, and using variable names that are descriptive:
void PrintMatrix(int matrix[10][10]; const int numberOfRows, const int numberOfColumns)
{
for(int row = 0; row < numberOfRows; row++)
{
cout<<endl;
for(int column = 0; column < numberOfColumns; column++)
{
cout<<a[i][j]<<" ";
}
}
}
daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
Yes, i want the space to be inserted when you press enter, without jumping to the next line. And when the maximum number of columns in a row is reached, it should jump on to the next line ie. the new row....
Assuming you want the console to auto-format itself according to the size of your matrix, this cannot be done without taking control of the console in a non-standard way. It's not terribly difficult, but I'd question the practicality of replacing the console with your own custom shell for the sole purpose of formatting matrix input.
Here's a very naive example of the concept:
#include <cctype>
#include <iostream>
#include <sstream>
#include <string>
#include <conio.h>
using namespace std;
string raw_gets()
{
string result;
int ch;
// Naive algorithm, use at your own risk
while (!isspace(ch = getch())) {
result += (char)ch;
cout << (char)ch;
}
return result;
}
int main()
{
int mat[2][4];
cout << "Enter the matrix:\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
stringstream conv(raw_gets());
int x;
conv >> x;
mat[i][j] = x;
cout << (j < 3 ? ' ' : '\n');
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++)
cout << mat[i][j] << ' ';
cout << '\n';
}
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
But why did you ask me to use at my risk?
Because it's example code and not robust enough to be usable in any kind of real program.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
But does that mean such a automatic aligning of the matrix in CONSOLE Application is impossible, No other robust method?
No, it just means I had neither the time nor the inclination to write a robust function for you when I think the whole concept is stupid. raw_gets() is a good starting point for adding the necessary checks and features to make it robust, but I'll leave that up to you.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Thank You very much for your help. I will try to carry on from here. Still, is such an concept all that useless? Got the idea, when one of my colleague (No touch with C++), ran my program and was quiet confused with the matrix. So just wanted to make it user-friendly.
Anyway Thanks again.
Yes it might be fun and user friendly but only for a while... It's not like your gonna use the program or use it as a basis for another program years for now
....then again who knows I could be wrong ;)
Instead you could also show the user the position they're inputting the number like e.g
"Enter number for row 1 column 1:"
To make it a bit more user friendly without going to all the trouble your doing right now
Just a suggestion :)
zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
Still, is such an concept all that useless?
Yes. If you want GUI behavior, write a GUI. Forcing the console to act like a GUI is awkward and confusing to users.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401