2016-05-15 8 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

ありがとう! */" 標準入力バッファから改行文字を削除します* /"それを言うには単純な "方法? –

+0

@EricYehこれは簡単な方法だと思います。 「シンプル」を定義してください。 – MikeCAT

+0

私たちは初心者としてgetcharとEOFについて学んでいないので、そうする別の方法があるのだろうかと思います。 –

関連する問題