为什么我的C程序在编译器中运行正常,但它们的.exe文件运行后会崩溃?

huangapple go评论72阅读模式
英文:

Why are my C programs running perfect in the compiler but the .exe of them runs then crashes?

问题

我已尝试运行多个在我的编译器(CodeBlocks)中运行并正常工作的程序的.exe文件,但当我运行文件的.exe时,它们开始运行,但最终崩溃。例如,这个计算器的.exe在应该显示答案时崩溃,但在我的编译器中正常工作。

#include <stdio.h>
#include <stdlib.h>;

float Add(float num1, float num2){
    return num1 + num2;
}

float Subtract(float num1, float num2){
    return num1 - num2;
}

float Multiply(float num1, float num2){
    return num1 * num2;
}

float Divide(float num1, float num2){
    return num1 / num2;
}

int main()
{
    while(1){

        int selection;
        float num1, num2, answer;

        char* selectionNames[] = {"Addition", "Subtraction", "Multiplication", "Division"};
        printf("Select Function from List:\n1: Addition\n2: Subtraction\n3: Multiplication\n4: Division\nSelection: ");
        scanf("%d", &selection);
        if(selection < 1 || selection > 4) {
            printf("Please choose a valid operator!");
            return 1;
        }
        if (selection == 0) {
            printf("Exiting...\n");
            break;
        }
        printf("You have Selected %s\n\n", selectionNames[selection-1]);

        printf("Enter First Number: ");
        scanf("%f", &num1);
        printf("Enter Second Number: ");
        scanf("%f", &num2);

        switch (selection){
        case 1:
            answer = Add(num1,num2);
            printf("\nAnswer = %.2f", answer);
            break;

        case 2:
            answer = Subtract(num1,num2);
            printf("\nAnswer = %.2f", answer);
            break;

        case 3:
            answer = Multiply(num1,num2);
            printf("\nAnswer = %.2f", answer);
            break;

        case 4:
            if (num2 == 0) {
                    printf("Cannot divide by zero!\n\n");
                    continue;
            }
            answer = Divide(num1,num2);
            printf("\nAnswer = %.2f", answer);
            break;
        }

        return 0;
    }
}

这个程序在我的编译器中工作正常,但它的.exe文件在显示答案之前崩溃,崩溃前需要输入3个用户输入。

英文:

I have tried running the .exe of multiple programs that run and work perfect in my compiler (codeblocks) but when I run the .exe of the file they begin running but crash eventually. The .exe of this calculator for example crashes when it is supposed to display the answer, but works fine in my compiler.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
float Add(float num1, float num2){
return num1 + num2;
}
float Subtract(float num1, float num2){
return num1 - num2;
}
float Multiply(float num1, float num2){
return num1 * num2;
}
float Divide(float num1, float num2){
return num1 / num2;
}
int main()
{
while(1){
int selection;
float num1, num2, answer;
char* selectionNames[] = {&quot;Addition&quot;, &quot;Subtraction&quot;, &quot;Multiplication&quot;, &quot;Division&quot;};
printf(&quot;Select Function from List:\n1: Addition\n2: Subtraction\n3: Multiplication\n4:     Division\nSelection: &quot;);
scanf(&quot;%d&quot;, &amp;selection);
if(selection &lt; 1 || selection &gt; 4) {
printf(&quot;Please choose a valid operator!&quot;);
return 1;
}
if (selection == 0) {
printf(&quot;Exiting...\n&quot;);
break;
}
printf(&quot;You have Selected %s\n\n&quot;, selectionNames[selection-1]);
printf(&quot;Enter First Number: &quot;);
scanf(&quot;%f&quot;, &amp;num1);
printf(&quot;Enter Second Number: &quot;);
scanf(&quot;%f&quot;, &amp;num2);
switch (selection){
case 1:
answer = Add(num1,num2);
printf(&quot;\nAnswer = %.2f&quot;, answer);
break;
case 2:
answer = Subtract(num1,num2);
printf(&quot;\nAnswer = %.2f&quot;, answer);
break;
case 3:
answer = Multiply(num1,num2);
printf(&quot;\nAnswer = %.2f&quot;, answer);
break;
case 4:
if (num2 == 0) {
printf(&quot;Cannot divide by zero!\n\n&quot;);
continue;
}
answer = Divide(num1,num2);
printf(&quot;\nAnswer = %.2f&quot;, answer);
break;
}
return 0;
}
}

The program works fine in my compiler but the .exe file for it crashes before it displays the answer, it takes 3 user inputs before the crash.

答案1

得分: 0

  1. 你的程序应该在一个循环中运行(否则为什么会有 while(1)?),然而,你在循环块内部有一个 return 语句(将其移到外部)。请注意,这不是通常理解的“崩溃”,只是一个导致不希望的行为的错误。

  2. 当你双击运行控制台应用程序时,窗口会在程序执行完毕后立刻关闭(这在你的IDE上不会发生,因为你有一个永久的终端)。窗口突然关闭可能被视为应用程序“崩溃”的迹象,但在这种情况下并非如此。在程序结束之前添加一个简单的提示可能是个好主意,以便让用户有机会阅读程序的最后信息:

int main() {
  // ...
  printf("【按回车键继续】");
  getchar();
  return 0;
}
英文:

There are two different problems:

  1. Your program is supposed to run in a loop (why would there be a while(1) otherwise?), however, you have a return statement inside the loop block (just put it outside). Note that this isn't what is generally understood as "crashing", and its just a bug causing undesired behavior.

  2. When you run a console application by clicking twice on it, the window will immediately close as soon as the program is done with its execution (which doesn't happen on your IDE because you have a permanent terminal). This abrupt window closing may be seem as a sign that the app is "crashing", but in this case it is not. It may be a good idea to add a simple prompt before the end of the program, to give your users a chance to read your program's last words:

int main() {
  // ...
  printf(&quot;[press enter]&quot;);
  getchar();
  return 0;
}

huangapple
  • 本文由 发表于 2023年3月7日 06:59:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定