所以我的程序设置为满足捐赠请求,我正在尝试制作一个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;
}
答案 0 :(得分:1)
你想要这个吗?
if (request[i] > donations[i] && donations[i] == 0)