我在c中有一个三角形程序
#include <stdio.h>
// A function which decides the type of the triangle and prints it
void checkTriangle(int s1, int s2,int s3)
{
// Check the values whether it is triangle or not.
if ((s1 + s2 > s3 && s1 + s3 > s2 && s2 + s3 > s1) && (s1 > 0 && s2 > 0 && s3 > 0))
{
// Deciding type of triangle according to given input.
if (s1 == s2 && s2 == s3)
printf("EQUILATERAL TRIANGLE");
else if (s1 == s2 || s2 == s3 || s1 == s3)
printf("ISOSCELES TRIANGLE\n");
else
printf("SCALENE TRIANGLE \n");
}
else
printf("\nTriangle could not be formed.");
}
int main(void)
{
// Initializing variables
int a,b,c;
// Getting input from user
printf("Please enter the sides of triangle");
printf("\nPlease enter side 1:");
scanf("%d",&a);
printf("Please enter side 2:");
scanf("%d",&b);
printf("Please enter side 3:");
scanf("%d",&c);
// Calling function in order to print type of the triangle.
checkTriangle(a,b,c);
}
当输入为:
7b
它给出了一个错误,这是我想要的,但是当我输入这样的数据时:
7
7
7b
它忽略 'b' 并将 7 作为整数 - 但为什么呢?我怎样才能防止这种情况?
我想要做的是也给出一个错误
7
7
7b