Command Line Issue

So I am almost done with my matrix multiplication program, but I was thinking of different tests that would make my program crash. The one test I can't figure out how to fix is this. First the program takes in the dimensions for two matrices through the command line. It then takes the numbers inside the matrices through the command line. The test is say someone put in for the numbers inside the matrices say a letter like "a". My program just output a bunch of zeros, and I would like to check the command line argument to make sure it is an integer with a value greater than or equal to 0. But I am unsure of how to do that.

Thanks

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;

// Preconditions: m is a two-dimensional array, initialized
//				  the first dimension of m is [0, 1, ..., rows-1]
//				  the second dimension of m is [0, 1, ..., cols-1]
// Postconditions: m is output to the screen
void output(int **m, int rows, int cols);

// Postconditions: outputs the usage statement to the screen
void usage();

// Preconditions: m1, m2, and r are two-dimensional arrays
//				  m1 and m2 are initialized
//				  the first dimension of m1 is [0, 1, ... rrows-1]
//				  the second dimension of m1 is [0, 1, ... inner-1]
//				  the first dimension of m2 is [0, 1, ... inner-1]
//				  the second dimension of m2 is [0, 1, ... rcols-1]
//				  the first dimension of r is [0, 1, ... rrows-1]
//				  the second dimension of r is [0, 1, ... rcols-1]
// Postconditions: r = m1 x m2
void m_mult(int **m1, int **m2, int **r, int rrows, int rcols, int inner);

int main( int argc, char *argv[] )
{
    
// Bellow the program is assigning the matrices dimension to integer variables
    
    int m1r = atoi( argv[1] );
    
    int m1c = atoi( argv[2] );
    
    int m2r = atoi( argv[3] );
    
    int m2c = atoi( argv[4] );
    
    
// If the user does not enter in the correct number of command line arguments then the if function below will end the program and show the usage.
    
    if ( argc != 5 )
        
    {
        usage();
        
        return 0;
    }
    
// If the user tries to enter a dimension that is less than one the program will output the usage.
    
    if( ( m1r < 1 ) || ( m1c < 1 ))
        
    {
        usage();
        
        return -1;
    }
    
    else if(( m2r < 1 ) || ( m2c < 1 ))
        
    {
        usage();
        
        return -2;
    }
    
    
// The below else if checks to see if the two inner dimensions of the matrices are the same, if not...usage.
    
    else if( m1c != m2r )
    
    {
        cout << "Invalid Dimensions" << endl;
    
        return -3;
    }
    
// Below is the code where a pointer is directed to the array created to store the first matrices rows then columns. The for loop then put the integers into the array.
    
    int **m1;
    
        m1 = new int*[ m1r ];
        
        for ( int i=0; i<m1r; i++ )
            
        {
            m1[i] = new int[ m1c ];
            
                for ( int j=0; j<m1c; j++ )
                    
                    cin >> m1[i][j];
        
    
        }
    
// Below is the code where a pointer is directed to the array created to store the second matrices rows then columns.The for loop then put the integers into the array.
    
    int **m2;
    
        m2 = new int*[ m2r ];
        
        for ( int i=0; i<m2r; i++ )
            
        {
            m2[i] = new int[ m2c ];
            
                for ( int j=0; j<m2c; j++ )
                    
                    cin >> m2[i][j];
        }
        
    int **r;
    
        r = new int*[ m1r ];
    
    for ( int i=0; i<m2c; i++)
        
    {
        r[i] = new int[ m2c ];
    }
    
    m_mult( m1, m2, r, m1r, m2c, m1c);
    
        cout << endl;
    
    output( m1, m1r, m1c );
    
        cout << endl;
    
    output( m2, m2r, m2c );
    
        cout << "Equals:" << endl;
    
    output( r, m1r, m2c);
    
    for ( int i=0; i <m1r; i++ )
        
    {
        delete[] m1[i];
        
        delete[] r[i];
    }

        delete[] m1;

    for ( int i=0; i <m2r; i++ )
    {
        delete[] m2[i];
    }

        delete[] m2;

    return 0;
}

void usage()
{
    cout << "usage: matrix_mult <m1 rows> <m1 columns> <m2 rows> <m2 columns>" << endl;
}

void output(int **m, int rows, int cols)
{
    
    for ( int i=0; i<rows; i++ )
    {
        for ( int j=0; j<cols; j++ )
            cout << m[i][j] << " ";
        
        cout << endl;
    }
}

void m_mult(int **m1, int **m2, int **r, int rrows, int rcols, int inner)
{
    for( int i = 0; i < rrows; i++ )
        
        for( int j = 0; j < rcols; j++ )
            
        {
            r[i][j]=0;
            
            for( int k = 0; k < inner; k++ )
                
                r[i][j] += m1[i][k] * m2[k][j];
        }
}
Last edited on
Lines 31-37. You do dereference argv before you have ensured (on line 42) that those elements do exist. Do the line 42 block first.


You do use atoi http://www.cplusplus.com/reference/cstdlib/atoi/
There is also stoi http://www.cplusplus.com/reference/string/stoi/
See also strtol http://www.cplusplus.com/reference/cstdlib/strtol/
One could also use formatted input from istringstream http://www.cplusplus.com/reference/sstream/istringstream/

The thing that you should focus on on those documentation pages is what each of those do if the input is invalid. You don't want undefined behaviour and catching exceptions is probably too advanced for now.


Note. The standard is not strict about the range of return values (of main), but 0--127 is more portable than negative values.
Topic archived. No new replies allowed.