2015-09-07 47 views
-2

誰能幫我一個矩形區域。我無法做出萬無一失,即禁止輸入字母和負數。我創建第一個指代字母的條件時,我無法創建第二個條件。我怎麼能做到這一點,或者我必須從頭開始改變程序。我只需要找到一個矩形區域。矩形區域萬無一失

#include "stdafx.h" 
#include <stdio.h> 
#include <math.h> 
#include <locale.h> 
#include <Windows.h> 

int _tmain(int argc, char* argv[]) 
{ 
    printf("Area of a Rectangle. Press Enter to start\n"); 
    system("pause"); 

    float a, s, d; 

    do 
    { 
     fflush(stdin); 
     system("cls"); 
     printf("Enter the lengths of the rectangle\n"); 
     scanf_s("%f", &a); 
     scanf_s("%f", &s); 

     if (getchar() != '\n') { 
      while (getchar() != '\n') 
       ; 
      printf("Your data is wrong\n"); 
      system("pause"); 
      continue; 
     } 

     break; 
    } while (true); 

    d = a*s; 
    printf(" Area is %.1f ", d); 
    system("pause"); 
    return 0; 
} 
+1

聲明:'fflush(stdin)'在C標準中明確聲明爲不起作用。這是因爲'fflush()'僅適用於輸出流,而不適用於輸入流。建議使用:'while(int ch = getchar()&& ch!= EOF && ch!='\ n');' – user3629249

+1

變量名:'a''和'd'沒有意義。你的代碼應該總是使用有意義的名稱來用於索引/循環計數器變量以外的變量建議:rectHeight rectWidth rectArea。 – user3629249

+1

這一行:'printf(「輸入矩形的長度\ n」);'並不真正告訴用戶輸入什麼內容。建議:'printf(「輸入矩形的長度寬度\ n」);'始終檢查從scanf()函數系列調用返回的值,以確保操作成功。 – user3629249

回答

2

該程序將要求一個數值,並只接受正值。字母或符號的輸入將導致scanf()失敗,程序將清除輸入緩衝區並重試。在這個例子中,輸入字母'q'將退出程序。你可以適應你的情況。

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


int main() 
{ 
    int i = 0; 
    float n = 0.0f; 

    do { 
     printf("enter numeric value or q to quit\n"); 
     if (scanf("%f",&n) != 1) {// scan one float 
      while ((i = getchar ()) != '\n' && i != 'q') { 
       //clear buffer on scanf failure 
       //stop on newline 
       //quit if a q is found 
      } 
      if (i != 'q') { 
       printf ("problem with input, try again\n"); 
      } 
     } 
     else {//scanf success 
      if (n == fabs (n)) { 
       printf("number was %f\n", n); 
      } 
      else { 
       printf("positive numbers only please\n"); 
      } 
     } 
    } while (i != 'q'); 

    return 0; 
} 

這已經適應了上述功能。

#include <stdio.h> 

float getfloat (char *prompt, int *result); 

int main (int argc, char* argv[]) 
{ 
    float width, height, area; 
    int ok = 0; 

    do { 
     printf("\n\tArea of a Rectangle.\n"); 
     width = getfloat ("Enter the width of the rectangle or q to quit\n", &ok); 
     if (ok == -1) { 
      break; 
     } 
     height = getfloat ("Enter the height of the rectangle or q to quit\n", &ok); 
     if (ok == -1) { 
      break; 
     } 

     area = width * height; 
     printf(" Area is %.1f\n", area); 
    } while (1); 

    return 0; 
} 

float getfloat (char *prompt, int *result) 
{ 
    int i = 0; 
    float n = 0.0f; 

    *result = 0; 

    do { 
     printf("%s", prompt); 
     if (scanf("%f",&n) != 1) {// scan one float 
      while ((i = getchar ()) != '\n' && i != 'q') { 
       //clear buffer on scanf failure 
       //stop on newline 
       //quit if a q is found 
      } 
      if (i != 'q') { 
       printf ("problem with input, try again\n"); 
      } 
      else { 
       *result = -1; 
       n = 0.0f; 
      } 
     } 
     else {//scanf success 
      if (n == fabs (n)) { 
       *result = 1; 
      } 
      else { 
       printf("positive numbers only please\n"); 
      } 
     } 
    } while (*result == 0); 

    return n; 
} 
+0

謝謝,您的反饋 – George

1

一種用於浮法萬無一失scanf看起來像這樣

float getFloat() { 
    float in; 
    while(scanf("%f", &in) != 1 && getchar() != '\n') { 
     fflush(stdin); 
     fprintf(stderr, "Wrong input, enter again : "); 
    } 
    return in; 
} 

這對我的作品。您的代碼將簡化爲:

float a = getFloat(); 
float b = getFloat(); 
printf("Area is %.1f", a * b);