简体   繁体   English

输入后小程序崩溃

[英]Little program crashes after taking a input

I'm absolutely new to C language and computer science. 我对C语言和计算机科学绝对陌生。 I'm still learning how to code. 我仍在学习如何编码。 This down below is a little program I wrote which converts the height in cm to feet and inches, however, the terminal crashes after it takes the height_cm as an input. 下面是我编写的一个小程序,该程序将以厘米为单位的高度转换为英尺和英寸,但是,在将height_cm作为输入后,终端会崩溃。 I thought for a long time but cannot figure out the reason behind it. 我想了很长时间,但无法弄清楚其背后的原因。

#include <stdio.h>
int main(void)
{
    float height_cm;
    float feet=(int)(height_cm/2.54)/12;
    float inch=(height_cm/2.54);

    while(height_cm > 0)
    {
        printf("Enter a height in centimeters(<=0 to quit): ");
        scanf("%.2f", &height_cm);
        printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
    }
    printf("Bye");
    return 0;
 }

Your main problem: You are referencing a unassigned variable height_cm when you are calculating feet and inches . 您的主要问题:在计算feetinches时,引用的是未分配的变量height_cm This is going to produced a undefined behavior because the value in that variable is a junk value. 这将产生未定义的行为,因为该变量中的值为垃圾值。 The below snipped of code addresses some of you other problems, such as using %.2f in scanf , and performs your desired logic. 下面的代码片段解决了一些其他问题,例如在scanf使用%.2f并执行了所需的逻辑。

#include <stdio.h>
int main(void)
{
    float height_cm; // Currently junk value
    int feet;        // Currently junk value
    float inch;      // Currently junk value

    // Keep calculating feet / inches as long as the entered hight is positive
    do {
        printf("Enter a height in centimeters(<=0 to quit): ");
        scanf("%f", &height_cm); // Can only use "%.2f for printf not scanf"
        feet=(int)(height_cm/2.54)/12;
        inch=(height_cm/2.54);

        // Output results
        printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
    }
    while (height_cm > 0);

    printf("Bye");

    return 0;
 }

You're specifying %d for your feet variable which is quite clearly a float , though oddly it's a float based on a rounded value, so you could use int if you wanted. 您正在为您的feet变量指定%d ,这显然是一个float ,尽管奇怪的是它是基于舍入值的float ,因此您可以根据需要使用int

Turn on warnings like -Wall to be alerted to simple mistakes like this. 打开-Wall警告,以警告类似这样的简单错误。

You'll also want to check for uninitialized variables as these are the primary source of crashes. 您还需要检查未初始化的变量,因为这些是崩溃的主要来源。

You can't use while on a variable that isn't defined. 您不能使用while在一个没有被定义的变量。 You must define it first. 您必须先定义它。

Consider restructuring: 考虑重组:

float height_cm;

while (true)
{
    printf("Enter a height in centimeters(<=0 to quit): ");
    scanf("%.2f", &height_cm);

    if (height_cm > 0) {
      int feet = (height_cm/2.54)/12;
      float inch = (height_cm/2.54) % 12; // Don't forget to modulo

      printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
    }
    else {
      break;
    }
}

Due to how you're asking for input and looping around it you'll want to conditionally break rather than express the break condition in the while itself. 由于您要求输入和环绕它的方式,您将希望有条件地break而不是在while本身中表达中断条件。

As others have stated, you are checking height_cm before assigning it a value. 正如其他人所述,您正在检查height_cm然后再height_cm分配值。 You can make a simple change to execute the loop at least once prior to checking for 0 by replacing the while with a do ... while loop: 您可以到检查做一个简单的变化来执行循环至少一次前0通过更换whiledo ... while循环:

    #include <stdio.h>
    int main(void)
    {
    float height_cm;
    float feet=(int)(height_cm/2.54)/12;
    float inch=(height_cm/2.54);

    do 
        {
        printf("Enter a height in centimeters(<=0 to quit): ");
        scanf("%.2f", &height_cm);
        printf("%.2f = %d feet, %.2f inches", height_cm,feet,inch);
        } while(height_cm > 0);
    printf("Bye");
    return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM