0

我在这个程序中有一个运行时错误,它没有语法错误,但在运行时崩溃。我正在使用dev-c++ 4.9.9.2。我试图找到错误,但我找不到它。如果有人可以提供帮助,请找出错误并纠正我。

#include<iostream.h>


void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();

main()
{
      DisplayVUID();
      char a[20] = "mc123456789";
      DisplayReverse(a, 20 );
      StoreDiagonal();

system("pause");
}
void DisplayVUID()
{
     int i;
     char name[20] = "mc123456789";
     cout<<"My VU id is ";
     for(i=0;i<20;i++)
     {
          cout<<name[i];
     }
     cout<<endl;
}
void DisplayReverse(char a[], int arraysize)
{
     int i;
     cout<<"MY VU id in Reverse is ";
     for(i=arraysize-1;i>=0;i--)
     {
       cout<<a[i];
     }
     cout<<endl;                           
}
void StoreDiagonal()
{
     int a[9][9] ;
     int i;
     int row, col;
     for (i = 0; i<9;i++)
     {
         for(i=0;i<9;i++)
         {
         a[row][col] = 0;
         }
     }
a[1][1] = 1;
a[2][2] = 3;
a[3][3] = 0;
a[4][4] = 2;
a[5][5] = 0;
a[6][6] = 2;
a[7][7] = 3;
a[8][8] = 9;
a[9][9] = 8;
      for(i = 0 ; i < 9 ; i ++)
              {
                for( i = 0 ; i < 9 ; i ++)
                {
                cout<<a[row][col];
                }
              }
}
4

2 回答 2

4

Stackoverflow 上的事情不是这样的,从下一次开始努力自己做事情,做你的研究然后来这里。你的程序中似乎有很多错误,但我试图删除一些错误,最后,它适用于我的系统。我还通过您可以在程序中查看的评论推荐了一些好东西:

编辑:我注意到由于数组中未分配的空格而在反向函数中打印出一些未定义的字符串,但我现在已经更正了。

#include<iostream>
#include<stdlib.h>
using namespace std;// use namespace otherwise cout won't work


void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();

int main()// In C++ always declare a main function like this,its good habit
{
      int i=0;
      DisplayVUID();
      char a[20] = "mc123456789";
      while(a[i]!='\0')
       i++;// Did this to ensure that cout will print only up to the null character,earlier it was printing some undefined characters along with the data in the array.
      DisplayReverse(a, i );
      StoreDiagonal();

  system("pause");
  return 0;//return 0
}
void DisplayVUID()
{
  //int i;
     char name[20] = "mc123456789";
     cout<<"My VU id is ";
     //for(i=0;i<20;i++)// no need for this loop at least here
     //{
          cout<<name;
          //}
     cout<<endl;
}
void DisplayReverse(char a[], int i)
{
     cout<<"MY VU id in Reverse is ";
   //  for(i=arraysize-1;i>=0;i--)
     //{
       while(i--)
       cout<<a[i];//instead of the for loop traversing for the whole arraysize i have made it go up to only the null terminator this way it doesn't print undefined characters.
     //}
     cout<<endl;                           
}
void StoreDiagonal()// i don't understand by the way what this function is here for , its not an error though.
{
     int a[9][9] ;
     int i,j,c=1;
     //int row, col;// you didn't initialize row and column and placed it inside the loop 
     for (i = 0; i<9;i++)
     {
         for(j=0;j<9;j++)
         {
         a[i][j] = 0;
      if(i==j)
       {
       a[i][j]=c;//instead of manually assigning do it like this.
       c++;
       }

         }
     }
       for(i = 0 ; i < 9 ; i ++)
              {
                for( j = 0 ; j < 9 ; j ++)
                {

                  cout<<a[i][j];// the elements of the array don't display like a table , this is not an error but to make your output readable do it by your self

              }
              }
}
于 2013-06-28T10:38:08.990 回答
2
a[9][9]=8;

删除这条线,你会没事的。数组索引从 0 而不是 1 开始。

另外我想指出,在您的函数 DisplayVUID() 中将 i<20 更改为 a[i]!='\0' 因为 '\0' 之后的值将是垃圾值。

于 2013-06-28T10:04:39.150 回答