将我的代码拆分为函数

时间:2012-11-05 05:31:44

标签: c++ arrays function multidimensional-array

我需要用X标记用户输入的座位,然后用标记的座位再次显示飞机布局(阵列)并每次重复。这是我的代码,我有点卡住了。如果你能帮我把它剪成两个函数:第一个函数应该打印出数组,第二个函数应该是用户输入并标记座位然后调用上一个函数来刷新显示。

我的输出应该是这样的:

1 X B C D

2 A X C D

3 A B C D

4 A B X D

5 A B C D

6 A B C D

7 A B C D

我的代码是:

 #include <iostream>

    using namespace std;


    int main ()
    {
        int rowNumber=-1, columnNumber=-1;
        char columnLetter;
        char airplane[7][4] = {{'A', 'B', 'C', 'D'},
         {'A', 'B', 'C', 'D'},
         {'A', 'B', 'C', 'D'},
         {'A', 'B', 'C', 'D'},
         {'A', 'B', 'C', 'D'},
         {'A', 'B', 'C', 'D'},
         {'A', 'B', 'C', 'D'}};

         for (int i=0; i<7; i++)
         { 
             cout<<i+1<<"\t";

             for (int j=0; j<4; j++)
             {
                 cout<<airplane[i][j]<<"\t";   
             }

             cout<<"\n";
         }

       do
       {
       cout<<"Enter row number:"<<endl;
       cin>>rowNumber;
       }while ((rowNumber<1)||(rowNumber>7));

       do
       {
       cout<<"Enter column letter:"<<endl;
       cin>>columnLetter;
       }while (columnLetter!='A'&&columnLetter!='B'&&columnLetter!='C'&&columnLetter!='D');

       if (columnLetter=='A')
       {
          columnNumber=0;
       }
       else if (columnLetter=='B')
       {
          columnNumber=1;
       }
       if (columnLetter=='C')
       {
          columnNumber=2;
       }
       else if (columnLetter=='D')
       {
          columnNumber=3;
       }

       airplane[rowNumber][columnNumber]='X';

           system ("PAUSE");
           return 0;
    }

我尝试了这个,但它没有编译,所以我无法移动到打印标记席位的下一个功能:

#include <iostream>

using namespace std;
void fillArray (char array[][4]);

int main ()
{
   char airplane[7][4] = {{'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'}};

fillArray (airplane[][4]);     


       system ("PAUSE");
       return 0;
}

void fillArray (char array[][4])
{


     for (int i=0; i<7; i++)
     { 
         cout<<i+1<<"\t";

         for (int j=0; j<4; j++)
         {
             cout<<array[i][j]<<"\t";   
         }

         cout<<"\n";
     }
     return;
}

    enter code here

2 个答案:

答案 0 :(得分:0)

第16行应该是fillArray (airplane);而不是fillArray (airplane[][4]);

答案 1 :(得分:0)

#include <iostream>

using namespace std;

void fillArray (char array[][4]);
void getSeat (char array[][4]);

int main ()
{
   char airplane[7][4] = {{'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'},
     {'A', 'B', 'C', 'D'}};

     fillArray (airplane);
     getSeat (airplane);
     fillArray (airplane);



       system ("PAUSE");
       return 0;
}

void fillArray (char array[][4])
{


     for (int i=0; i<7; i++)
     { 
         cout<<i+1<<"\t";

         for (int j=0; j<4; j++)
         {
             cout<<array[i][j]<<"\t";   
         }

         cout<<"\n";
     }
     return;
}

void getSeat (char array[][4])
{
     int rowNumber=-1, columnNumber=-1;
    char columnLetter;
  do
   {
      cout<<"Enter row number:"<<endl;
      cin>>rowNumber;
      rowNumber=rowNumber-1;
   }while ((rowNumber<1)||(rowNumber>7));

   do
   {
      cout<<"Enter column letter:"<<endl;
      cin>>columnLetter;
   }while (columnLetter!='A'&&columnLetter!='B'&&columnLetter!='C'&&columnLetter!='D');

   if (columnLetter=='A')
   {
      columnNumber=0;
   }
   else if (columnLetter=='B')
   {
      columnNumber=1;
   }
   if (columnLetter=='C')
   {
      columnNumber=2;
   }
   else if (columnLetter=='D')
   {
      columnNumber=3;
   }

   array[rowNumber][columnNumber]='X';
   return;
}