Reputation: 13
UPDATE: The code now runs perfect
close UPDATE.
You'll need to account for two different cases: buying an even number of milk cartons and buying an odd number of milk cartons. How can you determine if a number is even or odd?
This is what I have written so far and I would like some guidance please. I hope I'm making sense.
if (milk_boxes % 2 == 0)
total = milk_boxes * milk_price / 2;
else
total = (milk_boxes - 1) * milk_price / 2 + milk_price;
Upvotes: 1
Views: 130
Reputation: 362
I can see one issue in your code. You are calculating and displaying the price if the containers which are in odd numbers. But I think you forget to calculate the price of the containers if they are even in numbers, You are just printing value of the variable OJ_containers. You have to calculate and display it also.
As you are looking for the guidelines, I would suggest you should follow the Microsoft's coding guidelines :
The Microsoft's coding guidelines
I know it is for c# but you can still use those guidelines for other programming languages also. Many things are in common to other programming languages. like concept of variables, functions- in c# they call it as method. Hope you like it. as you progress you will get to know there are many programming approaches but don't confuse, just follow the standard once and I would like you to do this from the beginning, it will help you to become a good programmer.
Try this code :
#include <stdio.h>
#include <stdlib.h>
int main() {
double OJ_price;
int OJ_containers;
printf("What is the cost of one container of OJ in dollars?\n");
scanf("%lf", &OJ_price);
printf("How many containers are you buying?\n");
scanf("%d", &OJ_containers);
if(OJ_containers % 2 == 0)
printf("The total cost is %1f\n", (OJ_containers*OJ_price)/2);
else
printf("The total cost is $ %.2f\n",
((OJ_containers/2)*OJ_price)+OJ_price);
return 0;
}
Upvotes: 1
Reputation: 225232
You're not dividing by 2 for the BOGO offer in your "odd" case. Example follows - I separated the printf
out to clarify things a bit.
double total;
if (OJ_containers % 2 == 0)
// even means simply divide full price by two
total = OJ_containers * OJ_price / 2;
else
// odd means half price for the pairs, plus one more bottle at full price
total = (OJ_containers - 1) * OJ_price / 2 + OJ_price;
printf("The total cost is %f\n", total);
Upvotes: 0