gpt4 book ai didi

代码未验证验证或正确循环

转载 作者:行者123 更新时间:2023-11-30 14:36:57 25 4
gpt4 key购买 nike

Use a while loop. Prompt a user to input a site’s expenditure and upon hitting the return key:

Ask if they want to add another expenditure with the options of "Y" or "N" (as long as the "continue_loop" variable is equal to "Y"). (Hint: The loop should end if the user input is anything but "Y".)

Validate that the data entered is not below zero (review Chapter 4, Program 4-16 to see an example of input validation). If the value is below 0, present a message that says "Values must be greater than 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;
}

最佳答案

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

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

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

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

最后,使用索引检查站点的值(value)可能比 +i 更容易。 site[i] 以及获取输入时 &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;
}

关于代码未验证验证或正确循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57742268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com