1

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

在此处输入图像描述

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

2 回答 2

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

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

于 2013-04-15T20:22:32.577 回答
1

例如)

#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();
}
于 2013-04-16T12:06:37.243 回答