vendredi 24 juillet 2020

Correctly display grid map when two digits involve?

I have the following code:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int length, height;
    int counter = 0;
    int counter1 = 0;
    
    cout << "enter length : ";
    cin >> length;
    
    cout << "enter height : ";
    cin >> height;
    
    for (int row = 0; row <= height+3; row++)
    {
        for(int col = 0; col <= length+3; col++)
        {
            if ((col == 0) && (row == 0) || (col == 0) && (row == height+2))
                cout << "h  ";
            else if ((col == 0) && row >= 1 && row < height+3)
                cout << counter++ << "  ";
            else if ((col == 1) && (row == height+3) || (col == length+3) && (row == height+3))
                cout << "q  ";  
            else if ((col >= 2) && row == height+3)
                cout << counter1++ << "  ";
            else if ((row == 0) || (row == (height+2)) || (col == 1) || (col == (length+3)))
                cout << "x  ";
            else
                cout << "   ";
        }
       cout << endl;
    }
}

When I insert a length and height of 20, the grid map will not display properly due to 2 digit value. The result become like this:

enter length : 13                                                                                                               
enter height : 13                                                                                                               
h  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x                                                                               
0  x                                            x                                                                               
1  x                                            x                                                                               
2  x                                            x                                                                               
3  x                                            x                                                                               
4  x                                            x                                                                               
5  x                                            x                                                                               
6  x                                            x                                                                               
7  x                                            x                                                                               
8  x                                            x                                                                               
9  x                                            x                                                                               
10  x                                            x                                                                              
11  x                                            x                                                                              
12  x                                            x                                                                              
13  x                                            x                                                                              
h  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x                                                                               
   q  0  1  2  3  4  5  6  7  8  9  10  11  12  13  q

The expected result should be:

enter length : 13                                                                                                               
enter height : 13                                                                                                               
h  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x                                                                               
0  x                                            x                                                                               
1  x                                            x                                                                               
2  x                                            x                                                                               
3  x                                            x                                                                               
4  x                                            x                                                                               
5  x                                            x                                                                               
6  x                                            x                                                                               
7  x                                            x                                                                               
8  x                                            x                                                                               
9  x                                            x                                                                               
10 x                                            x                                                                              
11 x                                            x                                                                              
12 x                                            x                                                                              
13 x                                            x                                                                              
h  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x  x                                                                               
   q  0  1  2  3  4  5  6  7  8  9  10 11 12 13 q

I am not sure on how to take care of the 2 digits inputs. Will appreciate if someone can advice on this matter. Thanks!

Aucun commentaire:

Enregistrer un commentaire