gpt4 book ai didi

c - 尝试用C语言声明吗?

转载 作者:行者123 更新时间:2023-11-30 20:45:35 25 4
gpt4 key购买 nike

C中有try语句吗?我想制作一个梯形的“面积计算器”。我希望用户只输入数字:

#include <stdio.h>
#include <conio.h>

main()
{
clrscr();
float base1, base2, height, area;
printf("This program will compute the area of your trapezoid. Enjoy!");
printf("\n\tPlease enter the length of base1: ");
scanf("%f", &base1);
printf("\tPlease enter the length of base2: ");
scanf("%f", &base2);
printf("\tPlease enter the length of height: ");
scanf("%f", &height);
area = (base1 + base2) * h / 2;
printf("\nThe area of your trapezoid is: ", area);
getch();
}

但是,他们可以自由输入非数字数据。这样做会使程序崩溃。我可以做什么来检查数据是否正确?我相信这可以通过循环来完成,但由于一个程序有很多解决方案,我怀疑我的解决方案是最有效的。

最佳答案

C 没有异常,因此没有 try 语句,它是一项 C++ 功能,而不是 C 功能。您可以set up something similar yourself using longjmp()/setjmp() ,尽管我个人会避免尝试重新发明那个轮子。

关于c - 尝试用C语言声明吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7696068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com