2017-07-29 69 views
-1

當我運行這個程序(我使用的代碼塊和它的全面升級),它顯示一個對話框: 「」「」 .EXE已停止工作 問題導致程序無法正常工作。 Windows將關閉該程序並通知如果解決方案是可用的。「」「」.exe文件停止工作,當我運行一個C++程序(沒有「/ 0」)

#include <iostream> 

#include <math.h> 

#include <conio.h> 

using namespace std; 

int main() 
{ 

    int no, hlf, arr[no], arrno; 

    cout << "ENTER A NUMBER"; 

    cin >> no; 

    hlf = ceil(no/2); 


    for(int i = 1;i <= no;i++) 
    { 
     for(int j = 2;j <= hlf;j++) 
     { 
      int ij = i/j; 
      if(j != i && ij == 0) 
      { 
       goto cont; 
      } 
      else 
      { 
       continue; 
      } 
     } 
     arr[arrno] = i; 
     arrno++; 
     cont: ; 
    } 

    for(int k = 0;k <= arrno;k++) 
    { 
     cout << arr[k] << " "; 
    } 

    getch(); 
    return 0; 
} 
+8

這意味着你的程序崩潰*。你應該使用一個*調試器*來找出在哪裏,並幫助你找出原因。也許你也應該閱讀埃裏克利珀[如何調試小程序(https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+1

我建議你記下所有的變量及其值,然後在代碼更新時更新代碼。你會很快發現你的代碼存在問題。 –

+2

儘管如此:在沒有循環的情況下,程序執行從上到下。你的程序不會回頭去做一些事情,比如追溯重新定義變量。在初始化之前使用'no'時,請考慮這一點。 –

回答

0

有在你的代碼

  1. 沒有必要的#include <conio.h>getch();

  2. 陣列幾個錯誤arr[no]聲明是錯誤的。它應該是int arr[50];

這是更正的代碼運行良好。

#include <iostream> 
#include <math.h> 

using namespace std; 

int main() 
{ 

    int no, hlf, arrno; 
    int arr[50]; 


    cout << "ENTER A NUMBER"; 

    cin >> no; 

    hlf = ceil(no/2); 



    for(int i = 1;i <= no;i++) 
    { 
     for(int j = 2;j <= hlf;j++) 
     { 
      int ij = i/j; 
      if(j != i && ij == 0) 
      { 
       goto cont; 
      } 
      else 
      { 
       continue; 
      } 
     } 
     arr[arrno] = i; 
     arrno++; 
     cont: ; 
    } 

    for(int k = 0;k <= arrno;k++) 
    { 
     cout << arr[k] << " "; 
    } 


    return 0; 
} 

Here is the Screenshot, program runs just fine but i have not check your logic

+0

這裏是截圖,程序運行的很好,但我還沒有檢查你的邏輯:) –

+0

非常感謝你們這些傢伙eneryone – Sri

0

感謝球員,我得到了答案。這是我的不好,我沒有發佈,我需要打印素數。它是我在網絡論壇中的第一個問題。從未使用過。 PS - >再次感謝

包括

包括

使用命名空間std;

INT主要(){

int numb = 12, half; 

int arra[50], arrno = 0; 

half = ceil(numb/2); 

for(int r = 2;r <= numb;r++) 
{ 
    for(int t = 2;t <= half;t++) 
    { 
     if(r%t != 0 || t == r) continue; 
     else goto rpp; 
    } 
    arra[arrno] = r; 
    arrno++; 
    continue; 
    rpp: 
     continue; 
} 

for (int v = 0;v < arrno;v++) 
{ 
    cout << arra[v] << " "; 
} 
return 0; 

}

相關問題