当涉及两位数时正确显示网格图?

时间:2020-07-25 04:46:21

标签: c++ for-loop c++11 if-statement io

我有以下代码:

#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;
    }
}

当我插入20的长度和高度时,由于2位数的值,网格图将无法正确显示。结果变成这样:

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

预期结果应该是:

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

我不确定如何处理2位数的输入。如果有人可以就此问题提供建议,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用标题std::setw()中的std::left<iomanip>之类的操纵器,如下所示

productCategoryId

Live

另一件事Why is "using namespace std;" considered bad practice?