尝试使用scanf()接收多个变量[重复]

问题描述 投票:-2回答:1

此问题已经在这里有了答案:

因此,我决定增加一些编程技能,通常,我是一名应用程序支持分析师,对SQL编码技能的平均水平是初学者,但是在当今的环境下,我知道我需要在自动化简单任务方面变得更好。因此,我购买了C编程课程(这不是为了获得大学学分,因此要求我不要作弊)。到目前为止,它进行得很顺利,但是我遇到了一个问题,实际上我决定改进所面临的挑战,但似乎无法解决。最初,我被要求创建一个小型应用程序,该应用程序打印出矩形的宽度,高度,周长和区域,并以硬编码方式显示宽度和高度。这很容易弄清楚。我决定增加一个额外的挑战,并让最终用户输入矩形的宽度和高度,但是它不能按照我的计划工作。编译后,我将包括我的代码以及输出。我并不是很想要一个答案,因为我想自己找到解决方案,但是如果有人可以向我指出一些文档,将不胜感激。

CODE:

#include <stdio.h>
#include <stdlib.h>
int main()

{
    double width;
    double height;
    double perimeter;
    double area;
    char newLine = '\n';
    printf("Please enter the width of your rectangle:");
    scanf("%f", &width);
    printf("Please enter the height of your rectangle:"); 
    scanf("%f", &height);
    perimeter = 2.0 * (height + width);
    area = (width * height);
    printf("%c",newLine);
    printf("The widith of the Rectangle is: %.2f centimeters %c",width, newLine);
    printf("The height of the rectangle is: %.2f centimeters %c",height, newLine);
    printf("The permimeter of the rectangle is: %.2f centimeters %c",perimeter, newLine);
    printf("The area of the rectangle is: %.2f centimeters %c",area, newLine);
    printf("%c",newLine);
    return 0;
}

输出:

[[email protected]]$ ./output/rectangle.a 
Please enter the width of your rectangle:15
Please enter the height of your rectangle:12

The widith of the Rectangle is: 0.00 centimeters 
The height of the rectangle is: 0.00 centimeters 
The permimeter of the rectangle is: 0.00 centimeters 
The area of the rectangle is: 0.00 centimeters 
c scanf
1个回答
0
投票
您的代码很好,唯一的问题是您使用%f插入的%lf。 %f是浮点变量,%lf是双变量。

修复后程序的输出:

Please enter the width of your rectangle:25 Please enter the height of your rectangle:12 The height of the rectangle is: 12.00 centimeters The widith of the Rectangle is: 25.00 centimeters The permimeter of the rectangle is: 74.00 centimeters The area of the rectangle is: 300.00 centimeters

您可以使用此表来了解如何在c中使用各种数据类型:https://www.geeksforgeeks.org/data-types-in-c/

0
投票
您的代码很好,唯一的问题是您使用的是%f插入的%f。 %f是浮点变量,%lf是双变量。

修复后程序的输出:

Please enter the width of your rectangle:25 Please enter the height of your rectangle:12 The height of the rectangle is: 12.00 centimeters The widith of the Rectangle is: 25.00 centimeters The permimeter of the rectangle is: 74.00 centimeters The area of the rectangle is: 300.00 centimeters

您可以使用此表来了解如何在c中使用各种数据类型:https://www.geeksforgeeks.org/data-types-in-c/
© www.soinside.com 2019 - 2024. All rights reserved.