简单阵列坐标显示匹配

时间:2013-04-15 20:12:35

标签: c++ visual-studio visual-c++ multidimensional-array

我正在尝试创建一个数组,该数组接受用户输入并在99 99处中断,但多维数组不会停止获取输入值。另一个问题是在视觉表示中将值显示在网格下方。如果需要更多说明,请告诉我。

enter image description here

#include <iostream>
#include <ctime>

using namespace std;

int main()
{

srand(time(0));

int organisms[22][22];
int rows,
    columns;
int input[22][22];
int *output = new int[];

//filling array

for(int i=0;i<22;i++)
{

    for(int j=0;j<22;j++)
    {
        organisms[i][j]=0;
    }
}


//column output

cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tColumn\n\n";
for(columns=1;columns<21;columns++)
{
    cout<<"\t"<<columns;
}
cout<<"\n\n\n";

//Row and data output

    for(int i=0;i<20;i++)
    {

        cout<<"Row  "<<i+1<<"\t";

        for(int j=0;j<20;j++)
        {
            cout<<organisms[i][j]<<"\t";
        }
        cout<<endl;
    }
cout<<"\n\n\n";
cout<<"Enter each cell in first colony use row space column Enter format, 3 4, for example.\nEnter 99 99 to end entries.\n";

//input of values

cin.ignore(4);

int m=0;


do
{

    cin>>input[m][m];
    m++;

    if(input[m][m]!=99,99)
        break;

}while(m<20);



/*
while(input[m][m]!=99,99)
{
    cin>>input[m][m];
    m++;

}
 /*
for(int m=0;m<INT_MAX;m++)
{
    cin>>input[m][m];

    if(input[m][m]==99)
        break;
}
*/


//input array indexing






//output array

cout<<"\n\n\n";
cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tColumn\n\n";
for(columns=1;columns<21;columns++)
{
    cout<<"\t"<<columns;
}
cout<<"\n\n\n";

        for(int i=0;i<20;i++)
        {

        cout<<"Row  "<<i+1<<"\t";
        cout<<endl;

        for(int n=0;n<20;n++)
        {
            cout<<input[i][n]<<"\t";
        }

        cout<<endl;
}

cin.get();
cin.get();

return 0;
}

2 个答案:

答案 0 :(得分:2)

if(input[m][m]!=99,99)

您的输入是一个int数组,99,99不是int。使用有效的int值,例如5555或简单的99

答案 1 :(得分:1)

E.g)

#include <iostream>
#include <iomanip>
#include <utility>
#include <vector>
#include <iterator>

using namespace std;

const size_t size=20;

char table[size][size];

void table_clear(void){
    for(int v = 0; v<size; ++v)
        for(int h = 0; h<size; ++h)
            table[v][h]=' ';
}

void table_put(pair<int, int> p){
    int v = p.first;
    int h = p.second;
    table[v-1][h-1] = '*';//to zero origin
}

void table_disp(){
    cout << "   ";
    for(int i=0; i<size;++i){
        cout << setw(3) << i + 1;
    }
    cout << "\n" << endl;
    for(int i=0;i<size;++i){
        cout << setw(3) << left << i + 1;
        for(int j=0;j<size;++j){
            cout << setw(3) << right << table[i][j];
        }
        cout << endl;
    }
}

int main(){
    vector<pair<int,int> > v;
    pair<int, int> end(99,99);
    pair<int, int> rc;

    cout << "Enter each cell in first colony use row space column Enter format, 3 4, for example.\n";
    cout << "Enter 99 99 to end entries." << endl;
    while(true){
        int row, column;
        cin >> row;
        cin >> column;
        rc = make_pair(row, column);
        if(rc == end)
            break;
        v.push_back(rc);
    }

    table_clear();
    for(vector<pair<int,int> >::const_iterator iter = v.begin();iter != v.end(); ++iter ) {
        table_put(*iter);
    }
    table_disp();
}