2017-07-29 34 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; 

}

相关问题