Reputation: 5
I am writing a calorie program for my C programming class and when it compiles I don't get the correct output. For example I enter number of calories is 385 and get a total number of chips that exceed the calories I originally entered. I have included the code below.
Any help would be greatly appreciated.
#include "stdafx.h"
void calories(int total, int*pizza, int*chips, int*apple, int*mustard)
{
if (total >= 385)
*pizza = (total - 385);
if (total >= 170)
*chips = (total - (*pizza * 385))/170;
if (total >= 80)
*apple = (total - (*pizza * 385) - (*chips * 170)) / 80;
if (total >= 5)
*mustard = (total - (*pizza * 385) - (*chips * 170) - (*apple * 80)) / 5;
return;
}
int main(void)
{
int total, pizza, chips, apple, mustard;
printf("Enter the total whole number of calories you would like to eat for your meal: ");
scanf_s("%d", &total);
calories(total, &pizza, &chips, &apple, &mustard);
printf("\n Total= %d", total);
printf("\nSlices of pizza= %d", chips);
printf("\nBags of chips= %d", pizza);
printf("\nSlices of apple= %d",apple);
printf("\nTeaspoons of mustard= %d", mustard);
return 0;
}
Upvotes: 0
Views: 59
Reputation: 28305
On each if statement you are asking if total is greater yet you are not reducing value of total ... also looks like you wish to handle multiple quantities of pizza, etc yet you are not defining your if questions to accommodate this possibility
*pizza = total % 384;
total -= *pizza * 384;
*chips = total % 170;
total -= *chips * 170;
... do similar here for next purchase ...
... etc
Upvotes: 1
Reputation: 186
Error in printf statements :
printf("\nSlices of pizza= %d", chips);
printf("\nBags of chips= %d", pizza);
For printing pizza you gave chips value and vice-versa.
if (total >= 385)
*pizza = (total - 385);
Above you are not dividing total by 385 which may lead to wrong result.
Hope this will help.
Upvotes: 1
Reputation: 15310
To elaborate on @EdHeal's comment, you are declaring your variables pizza, chips, apple, mustard
on the stack. Stack variables in C are not automatically set to zero. You must initialize them to some value yourself.
You pass the address of the variables to the calories()
function, but inside that function you do not assign a value to the variables unless the calorie count is greater than a certain number. So whenever the calorie count is "too low", the variables will have random values in them.
The simplest solution would be to initialize the variables at declaration:
int pizza = 0, chips = 0, apple = 0, mustard = 0;
A less simple, but actually better, idea would be to add an else
clause to your statements in the calorie
function, and set the target values to zero if you don't have another value for them:
if (total >= 385)
*pizza = total / 385;
else
*pizza = 0;
/* etc ... */
Upvotes: 1
Reputation: 60017
I think this is the pattern you should adopt
if (total >= 385) {
*pizza = total / 385;
total = total - *pizza * 385;
}
if (total >= 170) {
*chips = total/170;
total = total - *chips * 170;
}
etc...
i.e. keep total the running total
PS: You should really get a better diet
Upvotes: 0