Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的多输入_C_Arrays_Cstdio - Fatal编程技术网

C语言中的多输入

C语言中的多输入,c,arrays,cstdio,C,Arrays,Cstdio,我正在尝试创建一个函数,该函数为电阻值获取多个输入。在我的主要功能中,程序要求用户询问需要多少电阻器。用户需要的电阻器数量将成为程序要求用户输入电阻值的次数。问题是我应该使用什么循环来生成错误语句并让用户再次输入值。输入仅接受整数。代码如下: #include <stdio.h> #include <ctype.h> #include <math.h> #include <string.h> #include

我正在尝试创建一个函数,该函数为电阻值获取多个输入。在我的主要功能中,程序要求用户询问需要多少电阻器。用户需要的电阻器数量将成为程序要求用户输入电阻值的次数。问题是我应该使用什么循环来生成错误语句并让用户再次输入值。输入仅接受整数。代码如下:

    #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);
        }
    }
#包括
#包括
#包括
#包括
#包括
#定义最大尺寸500
int电阻器(int重新计数,int*val_res);
int验证(字符*a)
{
无符号x;
对于(x=0;x100);
电阻器(numres,电阻);
返回0;
} 
int电阻器(int重新计数,int*val_res)
{
炭电阻_值[MAXSIZE];
intz;
printf(“请输入电阻器的值:\n”);
对于(z=1;z<(*val_res);z++)
{
printf(“\t请输入电阻器#%d:\n”,z);
如果(fgets(电阻值,sizeof(电阻值),标准输入)!=NULL)
{   
电阻器_值[strlen(电阻器_值)-1]='\0';
如果(验证(电阻器_值)==0)
{
val_res[z-1]=atof(电阻值);
}
}
做{
printf(“\t有效电阻\n”);
printf(“\t输入电阻器#%d:\n”,z);
如果(fgets(电阻值,sizeof(电阻值),标准输入)!=NULL)
{   
电阻器_值[strlen(电阻器_值)-1]='\0';
如果(验证(电阻器_值)==0)
{
val_res[z-1]=atof(电阻值);
}
}
}while(val_res[z-1]<0);
}
}
输出应如下所示:

您想在电路中放置多少个电阻器

三,

请输入电阻值:

请输入电阻#1:

abcd

无效抵抗

再输入电阻#1:

五,

请输入电阻#2:

六,


这不会发生在我的代码上。请帮忙,谢谢

代码中有两处更改

第一,

在循环中,您必须提到
重新计数
而不是
*val\u res

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

删除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");
            }
    }
z=1;
而(z<(*valu res))
{
printf(“\t请输入电阻器#%d:\n”,z);
如果(fgets(电阻值,sizeof(电阻值),标准输入)!=NULL)
{   
电阻器_值[strlen(电阻器_值)-1]='\0';
如果(验证(电阻器_值)==0)
{
val_res[z-1]=atof(电阻值);
z++;
}
其他的
{
printf(“\t有效电阻\n”);
}
}

您可以使用或。两者都可以用于转换和验证字符串。我已经完成了验证部分。我的问题是如何循环表示reinput阻力的语句。我只是说有更简单的方法来处理验证和转换。至于如何在验证失败时循环,没有“标准”的方式,你可以使用任何类型的循环你想要的,共同是例如<代码>(Sncf(抵抗,%d),和NoSRes!= 1){/*不正确的输入,再试*/}它不考虑错误的第一个输入。我应该把第一个FIX内的while循环?
    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");
            }
    }