簡體   English   中英

如何只接受C語言中的數字並阻止用戶輸入字母和特殊字符?

[英]How do I only accept numbers in C and block the user from entering letters and special characters?

我只需要弄清楚在用戶輸入的不是數字的情況下如何給出錯誤。 我已經設置了無法傳遞或無法通過的代碼值。

我只需要接受數字:如果輸入了字母或任何類型的特殊字符,我希望程序自行取消。 我怎樣才能做到這一點?

#include <stdio.h>
#include <math.h>

int main(void) {
    float base, height;
    float area;

    printf("Please enter the value of base of the triangle: \n");
    scanf ("%f", &base);
    if(base<.5)
        printf("Invalid Input\n");

    while (base<.5)
        return 0;

    if(base>100) 
        printf("Invalid Input\n");

    while(base>100)
        return 0;

    printf("Please enter the value of height of the triangle:\n");
    scanf("%f", &height);

    if(height<1)
        printf("Invalid Input\n");

    while(height<1)
        return 0;

    if(height>75)
        printf("Invalid Input\n");

    while (height>75)
        return 0;

    area = base * height/2;

    printf("The area of the triangle for base:  %f and height:  %f is %f \n", base,
            height , area );

    return 0;
}

您不能阻止用戶輸入他或她想要的任何內容,但是可以使用scanf的返回值來確定是否輸入了有效值,並提示用戶輸入正確的信息:

float base;
do {
    printf("Please enter the value of base of the triangle: \n");
} while (scanf ("%f", &base) != 1 || base < .5 || base > 100);

該循環將繼續直到滿足所有三個條件:

  • scanf返回了一項,
  • scanf提供的值大於或等於0.5 ,並且
  • scanf提供的值小於或等於100

scanf返回已成功匹配並填充值的變量數。 做就是了:

int result = scanf ("%f", &base);
if(result != 1)
{
    ...//handle error
}

我認為您可以逐字符讀取輸入的字符,然后檢查它是否為數字,如果不是,則顯示錯誤消息。

#include <stdio.h>
 #include <math.h>


int main(void)
{
    float base, height;
    float area;



printf("Please enter the value of base of the triangle: \n");
char c='\n';
char success=0;
base=0;
char dot=0;
do{
scanf ("%c", &c);
if((c>=48)&&(c<=57)){
  if(dot==0)
    base=base*10+(c-48);
  else{
    base+=(c-48)/dot;
    dot*=10;
  }
}
else
 if((c=='.')&&(dot==0))
     dot=10;
 else
   if(c!='\n')
     success=1;

}while((c!='\n')&&(succes==0));


if(success!=0) return -1; //error we break out
if(base<.5) 
printf("Invalid Input\n");
while (base<.5)
return 0;


if(base>100) 
printf("Invalid Input\n");
while(base>100)
    return 0;


printf("Please enter the value of height of the triangle:\n");
c='\n';
 success=0;
height=0;
 dot=0;
do{
scanf ("%c", &c);
if((c>=48)&&(c<=57)){
  if(dot==0)
    height=height*10+(c-48);
  else{
    height+=(c-48)/dot;
    dot*=10;
  }
}
else
 if((c=='.')&&(dot==0))//check if is the first time the user insert a dot
     dot=10;
 else
   if(c!='\n')
     success=1;

}while((c!='\n')&&(succes==0));


if(success!=0) return -1; //error we break out


if(height<1)
printf("Invalid Input\n");

while(height<1)
return 0;

if(height>75)
printf("Invalid Input\n");

while (height>75)
return 0;

area = base * height/2;

printf("The area of the triangle for base:  %f and height:  %f is %f \n", base,
height , area );

return 0;


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM