0

抵抗値の複数の入力を取得する関数を作成しようとしています。私の主な機能では、プログラムはユーザーに必要な抵抗の数を尋ねるように求めます。ユーザーが必要とする抵抗の数は、プログラムがユーザーに抵抗値の入力を求める回数になります。問題は、エラーステートメントを作成し、ユーザーに値を再度入力させるためにどのループを使用する必要があるかです。入力は整数のみを受け入れます。コードは次のとおりです。

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

    #define MAXSIZE 500
    int resistors(int rescount, int *val_res);
    int validate ( char *a )
    {
     unsigned x;
     for ( x = 0; x < strlen ( a ); x++ )
     if ( !isdigit ( a[x] ) ) 
        return 1;
     return 0;
    }

    int main()
    {
    int numres;
    int resistance[MAXSIZE];
    char resist[MAXSIZE];
    do
    {
        printf("How many resistors do you want to place in the circuit?\n");

        if (fgets(resist, sizeof(resist), stdin) != NULL)
        {
           resist[strlen (resist) - 1] = '\0';
           if(validate (resist) == 0)
           {
             numres = atoi(resist);
           }
        }   

    } while(numres < 1 || numres > 100);
    resistors(numres, resistance);
    return 0;
    } 

    int resistors(int rescount, int *val_res)
    {
        char resistor_value[MAXSIZE];
        int z;

        printf("Please input the value of resistors:\n");
        for(z = 1; z < (*val_res); z++)
        {
           printf("\tPlease input Resistor #%d: \n", z);
           if (fgets(resistor_value, sizeof(resistor_value), stdin) != NULL)
            {   
            resistor_value[strlen (resistor_value) - 1] = '\0';
            if(validate (resistor_value) == 0)
                {
                    val_res[z-1] = atof(resistor_value);
                }
            }
         do{
            printf("\tInvalid Resistance\n");
            printf("\tRe-input Resistor #%d: \n", z);
            if (fgets(resistor_value, sizeof(resistor_value), stdin) !=   NULL)
            {   
               resistor_value[strlen (resistor_value) - 1] = '\0';
               if(validate (resistor_value) == 0)
                {
                    val_res[z-1] = atof(resistor_value);
                }
            }

            }while(val_res[z-1] < 0);
        }
    }

出力は次のようになります。

回路にいくつの抵抗を配置しますか?

3

抵抗器の値を入力してください:

抵抗器 #1 を入力してください:

あいうえお

無効な抵抗です!

再入力抵抗 #1:

5

抵抗器 #2 を入力してください:

6

...

それは私のコードでは起こりません。助けてくださいありがとう!

4

2 に答える 2

0

コードの 2 つの変更。

最初の1つ、

rescount loop では、 not に言及する必要があります*val_res

 for(z = 1; z <= rescount; z++){
  . . . 
 }

次に、代わりにloop をdo..while使用whileしてエラーをチェックします。

  while(val_res[z-1] <= 0){
                    printf("\tInvalid Resistance\n");
                    printf("\tRe-input Resistor #%d: \n", z);
                    if (fgets(resistor_value, sizeof(resistor_value), stdin) !=   NULL)
                    {   
                            resistor_value[strlen (resistor_value) - 1] = '\0';
                            if(validate (resistor_value) == 0)
                            {
                                    val_res[z-1] = atof(resistor_value);
                            }
                    }

            }       
于 2015-04-17T05:01:56.200 に答える
0

do-while ループを削除します。不正な入力の場合は、その入力をカウントしないでください (z をインクリメントしないでください)。for ループを置き換えるコードを次に示します。

    z = 1;
    while(z < (*val_res))
    {
       printf("\tPlease input Resistor #%d: \n", z);
       if (fgets(resistor_value, sizeof(resistor_value), stdin) != NULL)
        {   
        resistor_value[strlen (resistor_value) - 1] = '\0';
        if(validate (resistor_value) == 0)
            {
                val_res[z-1] = atof(resistor_value);
                z++;
            }
            else
            {
                printf("\tInvalid Resistance\n");
            }
    }
于 2015-04-17T05:02:17.023 に答える