Originally Posted by
millcityrider
I have a feeling that the error is in my math, using too many parenthesis or something like
As a matter of fact, there is an extra parenthesis on line 22, which should have caused a compiler error. If not, you maybe want a better compiler. Another good thing for this is a syntax highlighting editor -- one that is savvy enough to catch uneven parentheses/braces.
I am using ideone online compiler,
What OS are you using? Why don't you just install a compiler? There are good quality free ones available, and it probably won't take you more than half an hour to get it going.
I get unexpected results and a handfull of scanf/format warnings.
Yes, your specifiers are backward. %d is for decimal integers (as in base 10 whole numbers, not decimals like 3.2), %lf is for doubles (as in "long" "floating point").
I am sure you want total to be a double too, not an int. BTW, using floating point for money is error prone because money is not really floating point, it is fixed precision, but for the purpose of your assignment let's put that aside.
How C treats numbers implicitly is important to understand. By "implicitly", I mean:
Code:
4 <-- treated as an int
4.0 <-- treated as a double
4.0f <-- treated as a float
This can affect the outcome of a calculation. For example:
x is not 1.333333, just because it is a float. 4 and 3 are integers, and dividing one integer by another integer in C yields an integer. So x is actually 1.000. Try it.
You can get out of that:
Code:
float x = (float)4/3;
or
float x = 4.0f/3;
I just mention this in case it turns out to effect some of your calculations. Pay attention to the implicit (and explicit) type of numbers.
One simple and obvious bad habit I see is repeating the same calculation:
Code:
printf("Food cost: %lf Cost of taxes: %lf total cost: %lf\n",
(ham * 3.5) + (fri * 1.75),
((ham * 3.5) + (fri * 1.75)) * .06,
(((ham * 3.5) + (fri * 1.75)) * .06) + ((ham * 3.5) + (fri * 1.75))
);
total += ((ham * 3.5) + (fri * 1.75)) * .06 + ((ham * 3.5) + (fri * 1.75));
Some reasons why this is bad:
- It's inefficient. That calculation must be done 6 times instead of just once (altho your compiler may be smart enough to spot this and optimize).
- It's confusing, because if the calculation is complex, it is not obvious that it is the same each time.
- It's error prone and awkward to develop/maintain. If the formula changes, you must change it in 6 different places, and you have 6 opportunities for typos or forgetfulness.
About point #1: some people may see repeating the calculation as having saved some memory, because you did not have to assign to another variable. That is backward. In general, it is preferable to use memory to save processing power, not use processing power to save memory.
If my issue is my understanding of float, double and int
The difference between a float and a double is that a double gets twice as much space in memory. So a double can hold larger/more precise values than a float.
Finally, I presume some of your formatting problems are due to cut n' paste, but in any case, that is no excuse: you should use proper style and make sure your posts come out that way too. It is not hard. WRT to your code, some of your lines are very long, try to break them, and place long comments on a line by themselves (further broken if required).
So here it is, the formatting cleaned-up, the repeat calculation removed, the scanfs fixed and total now a double. I'm not saying whether that means everything works correctly, but at least it will be easier for you to debug if you think the output is wrong.
Code:
#include<stdio.h>
int main(void) {
//This is what I came up with for variables. Using double because it was compiling better for me.
double ham=0;
double fri=0;
double tax=0;
int cust =0;
double total=0;
double base_cost;
//Used a while because it made sense and he hinted to wanting that on the assignment sheet,
//if ham is a negative number it should exit the loop
while(ham>=0) {
//Initially Fries, Hamburger and Tax will be set to zero,
//as it loops it will zero out again only recording number of customers and toal sale amount each time.
ham = 0, fri = 0, tax = 0;
printf("How many hamburgers would you like?:\n");
scanf("%lf",&ham);
printf("How many french fries would you like?:\n");
scanf("%lf",&fri);
base_cost = ham * 3.5 + fri * 1.75;
tax = base_cost * 0.06;
//The printf below is where I think my problem is. It makes sense to me,
//its basic math but I think it is programmed wrong and screwing up my code.
printf("Food cost: %lf Cost of taxes: %lf total cost: %lf\n",
base_cost, tax, base_cost + tax);
// This should store in "total" the amount plus tax
total += base_cost + tax;
// This should take "cust" which was previously set to zero up top and
// record each time inputs are entered until a negative number is placed in ham.
++cust;
}
//Once a negative number is placed in ham it should exit the while loop and printf the following,
//calling from the stored amount in total and cust
printf("Total amount of all sales: $%25.2lf Total number of customers: %d\n", total, cust);
return 0;
}
There is one repeated calculation in there (base_cost + tax), but I'm making a judgment call and considering it trivial.