【发布时间】:2020-01-15 23:28:07
【问题描述】:
我是编程世界的新手,最近我开始使用 C 进行编程,因此我编写了一个程序来确定一个数字是否完美。我使用 Code::Blocks IDE,它工作得很好,问题是当我单击“构建并运行”选项时,IDE 执行程序并且工作正常,但是当我从桌面选择 .exe 文件时,它打开,但不显示任何输出,窗口只是突然关闭。有人对如何解决这个问题有任何想法吗?
代码:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main() {
int N;
int j;
int Sum = 0;
printf("Input a number.\n");
scanf("%d",&N);
for (j = 1; j < N; j++)
{
if (N%j==0)
{
Sum+=j;
}
}
if (Sum==N)
{
printf("The number is perfect.\n");
} else {
printf("The number is not perfect.\n");
}
return 0;
}
Running program with Code::Blocks Build and run option
The only part of the Desktop located .exe that I can reach
如果有人可以提出解决方案,我将非常感激!
【问题讨论】:
-
从命令行而不是 gui 运行命令行程序。
-
您的程序一完成就退出。 Codeblocks 是一个调试器,因此它被设置为即使在程序退出后也保持窗口打开。但是,从 Windows UI 启动应用程序不会在程序退出后保持窗口打开(为什么要这样)。您的程序不需要立即退出,或者最好还是从命令行(例如 CMD 或 powershell)运行您的程序。
-
@kaylum OP 说它适用于 Code::Blocks。为什么不直接使用它?
-
@KeithThompson 我假设 OP 出于特定原因想要直接在 IDE 外部运行程序。但是,是的,如果它仍然是开发的一部分,那么继续在 Codeblocks 中运行将是一个好方法。