2015-09-07 45 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);