2015-08-08 88 views
-4

这是我在C代码,我不明白什么是错的。你可以看到有6个printf语句。我希望我的程序能够首先查找一个圆的面积和圆周,然后再查找一个正方形的面积和圆周,因此它会要求用户输入圆的直径,然后打印该面积和圆周,接下来它会请求用户输入正方形的长度,然后打印正方形的面积和周长。问题是当我运行我的代码时,它要求输入直径,然后打印区域和周长,然后在那里结束。它不要求输入正方形的长度。我的代码运行不正常

#include <stdio.h> 
    int main (void) { 
    //Circle 
    const float Pi = 3.142; 
    float radius = 0.0; 
    float dia = 0.0; 
    float area = 0.0; 
    float circum = 0.0; 
    printf("Input the diameter of the circle:"); 
    scanf("%f", &dia); 
    radius = dia/2; 
    area = radius*radius*Pi; 
    circum = 2*Pi*radius; 
    printf("\nThe area of the circle is %.2f",area); 
    printf("\nThe circumference of the circle is %.2f",circum); 
    //Square 
    float len = 0.0; 
    float areaS = 0.0; 
    float periS = 0.0; 
    printf("\nInput the length of the square:"); 
    scanf("%f",&len); 
    areaS = len*len; 
    periS = 4*len; 
    printf("The area of the square is %.2f", areaS); 
    printf("The perimeter of the square is %.2f", periS); 
    return 0; 
    } 
+3

怎么回事? –

+4

请您详细说明一下吗?你有构建错误吗?你有运行时错误(崩溃)?你有意想不到的输出? –

回答

0

那么,问题是返回0;

你离开的程序,你不能看到的结果,因为没有更多的scanf暂停。

你需要把一个的getchar或系统暂停返回0之前,你会看到结果。

+0

'问题是返回0'如何?为什么要删除'return 0'? – ameyCU

+0

那么,如果你在返回(0)之前放置一个系统(“暂停”),那么效果会更好。 – Braisly

+1

这是为什么会成为问题?如果从例如内部开始Visual Studio然后通常会在控制台程序完成后要求输入密钥,以便您可以看到输出。如果从控制台运行(在任何平台上),输出也会在那里。唯一出现问题的地方是,双击程序图标。我们*不知道*如果这是OP的实际问题。 –