【问题标题】:Code does not verify validation or loop properly代码未正确验证验证或循环
【发布时间】:2019-08-31 23:35:24
【问题描述】:

使用 while 循环。 提示用户输入网站的支出并按回车键:

询问他们是否要使用“Y”或“N”选项添加另一个支出(只要“continue_loop”变量等于“Y”)。 (提示:如果用户输入不是“Y”,则循环应该结束。)

验证输入的数据不低于零(查看第 4 章,程序 4-16 以查看输入验证示例)。如果值小于 0,则显示一条消息“值必须大于 0”。

最初用 python 编码,现在必须用 C 重做代码。虽然我在 python 方面表现不错,但我在正常编码方面相当糟糕。

我的大部分代码都已关闭,并且能够以非循环形式正常工作,但我无法弄清楚 while 循环设置以使其验证 y/n 响应。 在核心,我需要以下 for 循环和验证

#include <stdio.h>

int main()

{
    int site[20]={};

    int i = 0;

    int sum;

    int loop;

    int n;

    float avg;

    char continue_loop = "y";

    printf("Enter Site Expenditure:  \n");
    scanf(" %d",(site+i));
    ++i;
    printf("Do you want to enter another expenditure? Y/N \n");
    scanf(" %c",&continue_loop);

    while (continue_loop == "y");
    {
    printf("Enter Site Expenditure:  \n");
    scanf(" %d",(site+i));
    ++i;
    printf("Do you want to enter another expenditure? Y/N \n");
    scanf(" %c",&continue_loop);

    }
    printf("\nSite Expenditures \n");
    for(loop = 0; loop < i; loop++)
    printf(" %d \n", site[loop]);


    sum = avg = 0;

    for(loop = 0; loop < 3; loop++) {
    sum = sum + site[loop];
   }

    avg = (float)sum / loop;
    printf("Average of array values is %.2f ", avg);



    if (avg <= 35000)
    {
        printf ("The average site expenditure is meeting the organization's target goals!");
    }
    else {
        printf("The average site expenditure is NOT meeting the organization's target goals.");
    }
    return 0;
}

【问题讨论】:

  • 尝试在启用警告的情况下进行编译 (gcc -Wall myprog.c)。另外,请了解在 C 中 'y'char 值)和 "y"(指向字符串的指针)之间存在很大差异。
  • 我已经在这方面工作了一段时间,并在几个站点上进行了示例,但程序在输入字母后立即结束,而不是循环。我通常不是编码人员,所以这让我发疯。
  • 如果出现提示时输入"No Thanks" 而不是N 会怎样?为什么? scanf 如何处理从stdin 中提取字符?为什么fgets() 是接受用户输入的首选方式?

标签: c validation while-loop


【解决方案1】:

需要用您的代码修复一些问题。 首先,在 C 中声明字符文字时,使用单个 '。 char c = 'c'; 虽然字符串文字是一个字符数组 "。char c[] = "hello";

接下来在使用 while 循环时,如果您知道循环必须至少执行一次,那么 do while 比 while 更好。使用 do while,可以在循环内更改条件,确保它至少执行一次。

验证输入时,它只是一个简单的 if 语句来检查值是否小于 0。在您的情况下,我们要确保用户只输入正数,如果他们输入负数,则应询问他们再次输入号码。

当使用 scanf 从用户那里获取输入时,请记住回车符留在输入流中,即“\n”。因此,当您再次从用户那里获取输入时,可以通过从流中提取最后一个字符并以非阻塞方式继续执行来满足条件。我们要确保在用户输入某个值后清除输入流。有几种方法可以做到这一点,我更喜欢使用 fgets 获取输入,但如果您使用的是 scanf,我只需使用 while(getchar()!='\n'); 清除输入

最后,使用索引而不是 +i 可能更容易检查站点的值。 site[i] 和接受输入时 &amp;site[i] 这是我处理的更改的代码。


#include <stdio.h>
int main()

{
    int site[20]={};
    int i = 0;
    int sum;
    int loop;
    float avg;
    char continue_loop = 'y';
    do
    {
        printf("Enter Site Expenditure:  \n");
        do{
            scanf("%d",&site[i]);
            if(site[i]<0){
                printf("enter a value greater than 0\n");
            }
        }while(site[i]<0);

        ++i;
        printf("Do you want to enter another expenditure? Y/N \n");
        while(getchar()!='\n');
        scanf("%c",&continue_loop);

    }while(continue_loop == 'y');

    printf("\nSite Expenditures \n");
    for(loop = 0; loop < i; loop++)
        printf(" %d \n", site[loop]);


    sum = avg = 0;

    for(loop = 0; loop < 3; loop++) {
        sum = sum + site[loop];
    }

    avg = (float)sum / loop;
    printf("Average of array values is %.2f ", avg);



    if (avg <= 35000)
    {
        printf ("The average site expenditure is meeting the organization's target goals!");
    }
    else {
        printf("The average site expenditure is NOT meeting the organization's target goals.");
    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-08-25
    • 2020-08-31
    • 1970-01-01
    • 2016-01-29
    • 2012-10-09
    相关资源
    最近更新 更多