Hello all, I had to made a code to read from an input file using structures. The input file was information relating to a car dealership. I didn't post the input file, but I can if you need me to. I had to calculate the total revenue, the cars that have years greater than 2000, the cars that have prices under 5000, the cars that were black, and the cars that were toyotas and red. I am having trouble with the total revenue, the cars that are black, and the cars that are toyotas and red, other than that my program works fine. Can you help me please?
With regards line 54, what is the difference between "sum = sum + mycar.revenue", and "sum = ++mycar.revenue"? ;-)
With regards your other questions, do the entries in your data file have the same case? (ie "black" not "Black" or "BLACK"?). A string comparison (such as "if (mycar.color == "black")...") will do an exact match only.
You just fixed my program. I actually don't understand the difference between "sum = sum +mycar.revenue" and "sum = ++mycar.revenue". If you don't mind could you take me to a link to explain it or explain it yourself. As far as for the strings it was written as "Black" not "black" lol I am so mad at myself I have been stuck on this for like three days. I have that correct now. Thank you so much I really appreciate it.
The "++" operator is known as the 'pre-increment operator' (or 'post-increment operator' if you use it after the variable). When used on a line by itself eg at line 57, it is equivalent to:
cnt2000 = 1 + cnt2000;
ie, "add 1 to the value of cnt2000, and store the value back in cnt2000".
You could also have said "cnt2000++;" which is equivalent to:
cnt2000 = cnt2000 + 1;
which will give the same result, because "+" is commutative (ie a+b is equal to b+a).
The time when the difference is important is when you are using the ++ as part of an expression. In this case, the "pre-increment operator" (ie ++mycar.revenue) will result in the incremented value being used in the expression, and the "post-increment operator" (ie mycar.revenue++) will result in the _original_ value being used in the expression.
Consequently, if you say "sum = ++mycar.revenue", then the program will take the value of mycar.revenue, add one to it, and put the value in sum. If you say "sum = mycar.revenue++", then the program will put the _current_ value of mycar.revenue into sum, then add one to it.
Using actual numbers, if mycar.revenue was 5000, say, then:
sum = ++mycar.revenue; // results in sum = 5001, mycar.revenue = 5001
sum = mycar.revenue++; // results in sum = 5000, mycar.revenue = 5001
(neither of which is what you want - but I hope you understand a little better now what ++ is doing for you -- it's useful for incrementing counters, but not much else ;-) ).
Now, after all that, you can probably see that "sum = sum + mycar.revenue" is the expression that you wanted, as it will produce the running total you're interested in.