2013-06-28 129 views
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]; 
       } 
       } 
} 
+1

你不使用命名空间标准,尝试'使用命名空间std;包含语句'后。 – 0decimal0

+1

下一次,先使用调试器,然后给我们打电话。 –

+0

@Sireiz你没有讨论有关这个问题的事情,你也没有接受任何答案,如果我说我不指望你接受我的答案,但是我们回答问题的时间,我会说谎。问题,你应该注意讨论它是否工作或没有:) – 0decimal0

回答

4

事情不工作#2这种方式,从明年起时间努力做的事情你自己,做你的研究,然后来here.There似乎是在你的计划中有许多错误,但我试图删除一些错误,最后,它适用于我的系统。我也推荐了一些很好的东西,通过你可以在程序中看到的评论:

编辑:我注意到一些未定义的字符串,因为未分配的空间在数组中打印出来的反向功能,但我现在已经纠正它。

#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 

       } 
       } 
} 
+1

对于您在代码中的评论+1,我不支持以这种方式回答(完整代码),但是由于您添加了评论,所以非常有帮助的回答。保持! –

+1

@GrijeshChauhan非常感谢!只需要像你这样的人这样的支持和指导。其实,即使我不想写这样的答案,但我认为OP在这里很天真,所以为什么不给他一个指导,通过代码本身的特定点。无论如何,我将从现在开始考虑这一点。再一次感谢你。 :) – 0decimal0

2
a[9][9]=8; 

删除此行,你将被罚款。数组索引从0开始不是1.

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