Josh Josh - 1 month ago 11
C Question

How to scan through all "else if" conditions - C

For my C assignment I'm required to input a donation amount, input requests, and fulfill requests. Basically I have an array named donType[i] with i ranging from 0 to 4. donType[0] represents protein requests, donType[1] represents dairy requests, dontype[2] represents grain requests, and so on as you will see in my code. If any donation type has an inventory of 0 (meaning no donations were added to the value of the array) then I want it to print "type donations cannot be fulfilled" where type represents the type of food (protein, dairy, grains, etc). If I leave all of the inventories set to 0, it will only print "Protein requests cannot be fulfilled" where it should print all the requests as not being able to be fulfilled. Here's a portion of my code:

if (donType[0] == 0)
printf("Protein requests cannot be fulfilled.\n");
else if (donType[1] == 0)
printf("Dairy requests cannot be fulfilled.\n");
else if(donType[2] == 0)
printf("Grain requests cannot be fulfilled.\n");
else if (donType[3] == 0)
printf("Vegetable requests cannot be fulfilled.\n");
else if (donType[4] == 0)
printf("Fruit requests cannot be fulfilled.\n");


So it stops after it scans that donType[0] is equal to 0. How can I make my code continue to scan through the else if statements? Keep in mind I'm new to this stuff so I don't need any complex answer. Thanks for any help!

Dai Dai
Answer

The conditions are not mutually exclusive, so just remove the else keyword so that they're all independent if statements.