2016-05-15 83 views
-2

我正在处理的程序输出该符号的直角三角形,其边数等于该数字。它应该做的是终止,如果你输入0,否则它应该再次要求一个新的输入。该符号的直角三角形,边数等于该数字

所以我的问题是如何让它终止,如果你输入0,否则要求另一个输入?我知道我可能需要使用while循环。但是,我该如何改变它?

这里是我的代码:

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

int main() 
{ 
    char s;   /*s is symbol (the input)*/ 
    int a, b, n; /*n is number of rows (the input)*/ 

    printf("Please type the symbol of the triangle:...\n"); /*Ask for symbol input*/ 
    scanf_s("%c", &s, 1); 
    printf("Please type a positive non-zero number between 5 and 35:...\n"); /*Ask for number of rows input*/ 
    scanf_s("%d", &n); 
    assert(n >= 5 && n <= 35); 

    for (a = 1; a <= n; a++) /*How many rows to display+create*/ 
    { 
     for (b = 1; b <= a; b++) 
     { 
      printf("%c", s); 
     } 
     printf("\n"); 
    } 
    system("PAUSE"); 
} 
+0

所以我的问题是,“如何在你输入0结束,否则它会再次询问新的输入。” 我知道我可能需要while循环使用。但是,我如何改变它? –

+1

不要在评论中提问。相反,请将其包含在您的实际问题中。 – Laurel

回答

0

您可以使用循环来做到这一点。

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

#ifndef _MSC_VER 
/* passing extra arguments to scanf is not harmful */ 
#define scanf_s scanf 
#endif 

int main(void) 
{ 
    char s;   /*s is symbol (the input)*/ 
    int a, b, n; /*n is number of rows (the input)*/ 

    do { 
     printf("Please type the symbol of the triangle:...\n"); /*Ask for symbol  input*/ 
     scanf_s("%c", &s, 1); 
     printf("Please type a positive non-zero number between 5 and 35:...\n");  /*Ask for number of rows input*/ 
     scanf_s("%d", &n); 
     if(n >= 5 && n <= 35) 
     { 
      for (a = 1; a <= n; a++)/*How many rows to display+create*/ 
      { 
       for (b = 1; b <= a; b++) 
       { 
        printf("%c", s); 
       } 
       printf("\n"); 
      } 
     } 
     while ((a = getchar()) != '\n' && a != EOF); /* remove the newline character from standard input buffer */ 
    } while (n != 0); 
    system("PAUSE"); 
} 
+0

谢谢! 我们还没有得知“while((a = getchar())!='\ n'&& a!= EOF);/*从标准输入缓冲区中删除换行符* /” 是否有任何“简单“的方式来说呢? –

+0

@EricYeh我认为这是一个简单的方法。请定义“简单”。 – MikeCAT

+0

因为我们还没有学过关于getchar和EOF作为初学者,所以我想知道是否有另一种方法来做到这一点。 –