2014-09-29 76 views
1

我只需要弄清楚如何給出錯誤是用戶輸入任何不是數字的東西。我已經設置了無法通過或不能通過的代碼的值。如何僅接受C中的數字並阻止用戶輸入字母和特殊字符?

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

#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; 
} 

回答

2

你不能阻止進入,無論他或她想要的用戶,但你可以使用的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
+0

由於OP想要「給出錯誤是(如果)用戶輸入任何不是數字的」,這裏的弱點在這裏。這不會完全消耗「123abc」之類的非digt文本,但將「abc」保留在「stdin」中並且不會出現錯誤。建議'fgets()'然後'sscanf()/ strtof'。 – chux 2014-09-29 19:26:30

0

scanf返回的已匹配和填充的變量數值成功。 只是做:

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

我認爲,你可以通過閱讀文字輸入的字符,然後檢查它是否是一個數字,如果不告訴你你的錯誤消息。

#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; 


} 
相關問題