2013-06-28 125 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'後的值將是垃圾值。