Array Help Please

Pages: 123
figured it out and it works great.

can you explain the string stream to me and what it does never used it.

Now i just need to figure out how to end the length of the matrix by just entering "enter"
on an empty row to end the array
Last edited on
> I am getting an compile error on
stringstream
You need to include the header <sstream>
Last edited on
> Can you explain the string stream to me
http://www.cplusplus.com/reference/sstream/stringstream
enter row 1 for the matrix: 1 1 1
enter row 2 for the matrix: 2 2 2
enter row 3 for the matrix: "Enter"

array would look like
111
222



how would i modify the code so that it just presets the column in the beginning.
and then the length will be determined and done by pressing "enter" on
the last empty line.


Sorry, you need some more clarifications
OUTPUT:

1
2
3
4
5
enter row 0 for the matrix: 3 3 3
The program has determined the matrix will have 3 columns. The size : 3 x 3

enter row 1 for the matrix: 1 1 1
enter row 2 for the matrix: 2 2 2



instead of The size : 3 x 3

I would like The size : 3 x Undetermined Until "Enter" on an empty line

enter row 1 for the matrix: 1 1 1
enter row 2 for the matrix: 2 2 2
enter row 3 for the matrix: 3 3 3
enter row 4 for the matrix: "Enter"

so now the matrix would end as a 3x3 and all values would be stored

pretty much keep prompting for rows untill user inputs "enter" on a blank line

enter row 1 for the matrix: 1 1 1
enter row 2 for the matrix: 2 2 2
enter row 3 for the matrix: 3 3 3
enter row 4 for the matrix: 4 4 4
enter row 5 for the matrix: 1 1 1
enter row 6 for the matrix: 2 2 2
enter row 7 for the matrix: "Enter"

so this would now be 3 x 6






Last edited on
Ok, that would be fine, but isn't it a little too advanced for an assignment? Is it your mid-term exam assignment or something?
No not a mid term. its an assignment that my teacher assigned. This is the only part im struggling with is the input of the matrix thats fixed by user inputting "Enter". Other wise it would be a piece of cake.
Last edited on
Can you show me how to end the array by the example above with Enter?
> enter row 0 for the matrix: 8 2 5 "Enter"
The program has determined the matrix will have 3 columns.

enter row 1 for the matrix: 0 1 0 "Enter"
enter row 2 for the matrix: 3 1 9 "Enter"
enter row 3 for the matrix: 5 2 6 "Enter"
enter row 4 for the matrix: "Enter"

The program has determined the matrix will have 4 rows. The size : 3 x 4


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
int numRows = 0, i = 0, j = 0, numCols = 0;

int matrix[1000][1000];

std::stringstream ss;
std::string str = "2 8 4";

cout << "enter row " << numRows << " for the matrix: ";

std::getline(std::cin, str);
ss.str(str);
while(ss >> matrix[numRows][numCols])++numCols; ++numRows;
cout << "The program has determined the matrix will have " << numCols;
cout << " columns.";
cout << endl << endl; 

while(1)
{
    cout << "enter row " << numRows << " for the matrix: ";
    getline(cin, str);
    ss.str(str); i = 0;
    while((ss >> matrix[numRows][i])) ++i;
   if(i == 0) break; 
   ++numRows;
}

cout << "The program has determined the matrix will have " << numRows << " rows. ";
cout << "The size : " << numCols << " x " << numRows << endl << endl;
Last edited on
1
2
3
4
5
6
7
8
for (index = 0; index < numRows; index++)
        {
            for (int j = 0; j < numCols; j++)
            {
                cout << matrix[index][j];
            }

        }


When i output the matrix i get

1
2
3
4
5
6
7
nter row 0 for the matrix: 1 2 3 4
The program has determined the matrix will have 4 columns.

enter row 1 for the matrix: 1 1 1 1
The program has determined the matrix will have 2 rows. The size : 4 x 2

12340000


What am i doing wrong here
While loop is ending right after second input

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <sstream>
using namespace std;




int main() {

    int numRows = 0;
    int numCols = 0;
    int index = 0;
    int j = 0;


    int matrix[1000][1000];

    stringstream ss;
    string str = "2 8 4";

    cout << "enter row " << numRows << " for the matrix: ";
    getline(std::cin, str);

    ss.str(str);
    while(ss >> matrix[numRows][numCols])++numCols; ++numRows;
    cout << "The program has determined the matrix will have " << numCols;
    cout << " columns.";
    cout << endl << endl;

    while(1)
    {
        cout << "enter row " << numRows << " for the matrix: ";
        getline(cin, str);
        ss.str(str);
        index = 0;

        while((ss >> matrix[numRows][index])) ++index;
        ++numRows;
        if(index == 0) break;
    }


    cout << "The program has determined the matrix will have " << numRows << " rows. ";
    cout << "The size : " << numCols << " x " << numRows << endl << endl;




        for (index = 0; index < numRows; index++)
        {
            for (int j = 0; j < numCols; j++)
            {
                cout << matrix[index][j];
            }

        }



}
Last edited on
Fixed a logic error. Now it is done.
I edited the ++numRows and now this is what i get

1
2
3
4
5
6
7
8
enter row 0 for the matrix: 1 1 1 1
The program has determined the matrix will have 4 columns.

enter row 1 for the matrix: 0 1 0 1
The program has determined the matrix will have 1 rows. The size : 4 x 1

1111
Process finished with exit code 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <sstream>
using namespace std;




int main() {

    int numRows = 0;
    int numCols = 0;
    int index = 0;
    int j = 0;


    int matrix[1000][1000];

    stringstream ss;
    string str = "2 8 4";

    cout << "enter row " << numRows << " for the matrix: ";
    getline(cin, str);

    ss.str(str);
    while(ss >> matrix[numRows][numCols])++numCols; ++numRows;
    cout << "The program has determined the matrix will have " << numCols;
    cout << " columns.";
    cout << endl << endl;

    while(1)
    {
        cout << "enter row " << numRows << " for the matrix: ";
        getline(cin, str);
        ss.str(str);
        index = 0;

        while((ss >> matrix[numRows][index])) ++index;
        if(index == 0) break;
        ++numRows;
    }


    cout << "The program has determined the matrix will have " << numRows << " rows. ";
    cout << "The size : " << numCols << " x " << numRows << endl << endl;




        for (index = 0; index < numRows; index++)
        {
            for (int j = 0; j < numCols; j++)
            {
                cout << matrix[index][j];
            }

        }



}
Last edited on
it counted the height right before just not registering it into the array its assigning values to zero with ++numRows before the break.

++numRows after the break is not working properly
closed account (48T7M4Gy)
.
Last edited on
Can you post your full output so we can better diagnose the program bugs.
For example : matrix 3x5

Also print out all matrix elements after inputting & let us know the output please
Last edited on
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <sstream>
using namespace std;




int main() {

    int numRows = 0;
    int numCols = 0;
    int index = 0;
    int j = 0;

    int matrix[1000][1000];

    stringstream ss;
    string str = "2 8 4";

    cout << "enter row " << numRows << " for the matrix: ";
    getline(cin, str);
    ss.str(str);

    while(ss >> matrix[numRows][numCols])++numCols; ++numRows;
    cout << "The program has determined the matrix will have " << numCols;
    cout << " columns.";
    cout << endl << endl;

    while(1)
    {
        cout << "enter row " << numRows << " for the matrix: ";
        getline(cin, str);
        ss.str(str);


       index = 0;
        

        while((ss >> matrix[numRows][index])) ++index;
        ++numRows;
        
        if(index == 0) break;

    }


    cout << "The program has determined the matrix will have " << numRows << " rows. ";
    cout << "The size : " << numCols << " x " << numRows << endl << endl;




        for (index = 0; index < numRows; index++)
        {
            for (int j = 0; j < numCols; j++)
            {
                cout << matrix[index][j];
            }

        }



}




Im trying to fix this code. I have error checked it over and over again but i cant seem to get it passed the second input because index is ==0 but i need the loop to end when "Enter" is inpute on an empty row
Last edited on
1
2
3
4
5
6
7
8
enter row 0 for the matrix: 1 2 3 4
The program has determined the matrix will have 4 columns.

enter row 1 for the matrix: 9 9 9 9
The program has determined the matrix will have 2 rows. The size : 4 x 2

12340000
Process finished with exit code 0


the loop is breaking after the second input in the second loop.
I need it to break when the row is empty not after integer input.
Last edited on
Ok, you may try this :
1
2
3
4
5
ss.str(""); 
ss.clear();
getline(cin, str);
ss.str(str);
index = 0;
Pages: 123