gpt4 book ai didi

c - 带数组的 If 语句。如何判断数组整数是否为0?

转载 作者:行者123 更新时间:2023-11-30 21:00:36 24 4
gpt4 key购买 nike

所以我的程序被设置为满足捐赠请求,我试图制作一个 if 语句,表示如果请求大于捐赠且捐赠为 0,则打印该请求无法满足。那么如何检查数组中的整数是否为0呢?程序中的案例3就是我的问题所在。谢谢

#include <stdio.h>

int main() {
int choice, i, number, type;
int temp_donations[5] = {0};
int donations[5] = {0};
int request[5] = {0};
char TYPES[5][20] = {"Protein", "Dairy", "Grains", "Vegetables", "Fruits"};

printf("Welcome to the Food Bank Management Program!\n");

//Give and ask for the user's choice
printf("What would you like to do?\n");
printf("\t1. Enter a Donation\n");
printf("\t2. Enter a Request\n");
printf("\t3. Fulfill Request\n");
printf("\t4. Print status report\n");
printf("\t5. Exit\n");
scanf("%d", &choice);
printf("\n");

//Print if choice is greater than 5 or less than 1
if(choice > 5 || choice < 1)
printf("Sorry, that was not a valid input.\n\n");

while (choice != 5) {
switch (choice) {
case 1:
//ask user the type of food they would like to donate
printf("\nWhat donation type would you like to enter?\n");
number = 0;
for(i=0; i<5; i++){
printf("\t%d. %s\n",number, TYPES[i]);
number += 1;
}
//user input for food type and amount to donate
scanf("%d", &type);
printf("How much would you like to donate? ");
scanf("%d", &donations[type]);
printf("Donation Added!\n\n");
break;
case 2:
//ask user the type of food they would like to request
printf("\nWhat would you like to request?\n");
number = 0;
for(i=0; i<5; i++){
printf("\t%d. %s\n",number, TYPES[i]);
number += 1;
}
//user input for request and amount requested
scanf("%d", &type);
printf("How much would you like to donate? ");
scanf("%d", &request[type]);
printf("Donation Added!\n\n");
break;
case 3:
//go through foods and fulfill the requests if possible
for(i=0; i<5; i++){
if (request[i] > donations[i] && //I'm not sure what to put in here)
printf("%s requests cannot be fulfilled.\n", TYPES[i]);
else if (request[i] > donations[i]){
printf("%s requests will be partially fulfilled.\n", TYPES[i]);
temp_donations[i] = donations[i];
donations[i] -= donations[i];
request[i] -= temp_donations[i];
}
else {
donations[i] -= request[i];
request[i] -= request[i];
}
}
printf("\n");
break;
case 4:
//print table of current donations and requests
for(i=0; i<5; i++){
printf("\t%-10s: Donations: %-2d Requests: %-2d\n", TYPES[i], donations[i], request[i]);
}
printf("\n");
break;
}
//reask for user's choice
printf("What would you like to do?\n");
printf("\t1. Enter a Donation\n");
printf("\t2. Enter a Request\n");
printf("\t3. Fulfill Request\n");
printf("\t4. Print status report\n");
printf("\t5. Exit\n");
scanf("%d", &choice);
printf("\n");

if(choice > 5 || choice < 1)
printf("Sorry, that was not a valid input.\n\n");
}

printf("Thank you for running our system!\n");

return 0;
}

最佳答案

你想要这个吗?

if (request[i] > donations[i] && donations[i] == 0)

关于c - 带数组的 If 语句。如何判断数组整数是否为0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40301029/

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