【问题标题】:How would I be able to insert user input limits (max and min values) and reject non-numeric values in this code?我如何能够在此代码中插入用户输入限制(最大值和最小值)并拒绝非数字值?
【发布时间】:2022-12-08 06:07:38
【问题描述】:

我是初学者,不确定如何将这些插入到我的代码中。该代码允许您在星形和三角形电阻网络转换之间进行选择。还有一个退出选项。

我想在用户输入中添加一些验证,如果值不在范围内,错误代码将要求他们重新输入一个值。非数字输入也是如此。

如果有人可以告诉我如何将这些限制中的一个添加到我的代码中以便我可以自己尝试其他两个,将不胜感激。

我曾尝试使用 do while 循环,但它拒绝了所有输入而不是期望的结果。 我在某处读到我应该使用 flush 但我不知道如何执行此操作。

`

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



int main(void)
{
    printf("\n\n\t\tDelta and Star Converter\n\n\n");
    int choice, num, i;
    unsigned long int fact;

    while(1)
    {
        printf("1. Star \n");
        printf("2. Delta\n");
        printf("0. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);

     
        switch(choice)
        {
        case 1:;
        float R_a=0,R_b=0,R_c=0,R_ab,R_bc,R_ac;

        printf("Please enter the value of the Star connected resistors:\n");


        printf("R_a = ");
        scanf("%f",&R_a);
        printf("R_b = ");
        scanf("%f",&R_b);
        printf("R_c = ");
        scanf("%f",&R_c);


        R_ab=R_a+R_b+(R_a*R_b)/R_c;
        R_bc=R_b+R_c+(R_b*R_c)/R_a;
        R_ac=R_a+R_c+(R_a*R_c)/R_b;

        printf("the equivalent Delta values are: \n");
        printf("R_ab = %.2f Ohms\n",R_ab);
        printf("R_bc = %.2f Ohms\n",R_bc);
        printf("R_ac = %.2f Ohms\n",R_ac);
        break;

        case 2:;



        printf("Please enter the values of the Delta connected resistors:\n");

        printf("R_ab = ");
        scanf("%f",&R_ab);
        printf("R_bc = ");
        scanf("%f",&R_bc);
        printf("R_ac = ");
        scanf("%f",&R_ac);



        R_a = (R_ab*R_ac)/(R_ab + R_bc + R_ac);
        R_b = (R_ab*R_bc)/(R_ab + R_bc + R_ac);
        R_c = (R_ac*R_bc)/(R_ab + R_bc + R_ac);

        printf("the equivalent Star values are: \n");
        printf("R_a = %.2f Ohms\n",R_a);
        printf("R_b = %.2f Ohms\n",R_b);
        printf("R_c = %.2f Ohms\n",R_c);
        break;

            case 0:
                printf("\n\nAdios!!\n\n\n");
                exit(0);    // terminates the complete program execution
        }
    }
    printf("\n\n\t\t\tThank you!\n\n\n");
    return 0;
}








`

【问题讨论】:

    标签: c validation user-input limit converters


    【解决方案1】:

    有许多不同的方法来验证输入(您可以使用正则表达式检查,您可以简单地将返回值与预期条件进行比较)。如果您使用的是 input() 方法,您可以获取它的返回值,您知道它是一个字符串类型,并调用 isdigit() 方法来确认您是否得到了一个没有字符的有效数字。如果您期望像 1,2,3 这样的数字 isdigit() 将返回 true,否则它将返回 false,然后您可以重新提示用户。一些示例代码看起来像

    input_valid = False
    while(not input_valid):
      val = input("give me some input")
        if val.isdigit():
           # value was a digit do something
           input_valid = True
        else:
           print("you did not enter a valid entry, please re-enter value")
    

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2020-02-28
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 1970-01-01
      相关资源
      最近更新 更多