Strucutres and reading from file

Dec 12, 2011 at 6:27pm
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 
# include <cmath>
# include <iostream>
# include <iomanip>
# include <cstring>
# include <cctype>
# include <fstream>
# include <string> 

using namespace std; 

int main()
{
ifstream fin;
fin.open("fin.txt");

int cnt2000 = 0; 
int cnt5000 = 0; 
int cntcolor = 0; 
int cntcoma = 0; 
int sum = 0; 

struct car {
string id;
string make;
string model;
int year;
string color;
int purchase_month;
int purchase_day;
int purchase_year;
float cost;
int sold_month;
int sold_day;
int sold_year;
int sale;
int revenue;
}mycar;

cout << "ID " << "Make " << "Model " << "Year " << "Color " << "Purchased " << "Cost " << "Sold " << "Sale " << "Revenue " << endl; 
cout << "************************************************************************" << endl; 

while (fin)
{fin >> mycar.id >> mycar.make >> mycar.model >> mycar.year >> mycar.color >> mycar.purchase_month >> mycar.purchase_day; 
fin >> mycar.purchase_year >> mycar.cost >> mycar.sold_month >> mycar.sold_day >> mycar.sold_year >> mycar.sale; 

mycar.revenue = mycar.sale - mycar.cost; 

cout << mycar.id << " " << mycar.make << " " << mycar.model << " " << mycar.year << " " << mycar.color << " "; 
cout << mycar.purchase_month << "/" << mycar.purchase_day << "/" << mycar.purchase_year << " " << mycar.cost << " "; 
cout << mycar.sold_month << "/" << mycar.sold_day << "/" << mycar.sold_year << " " << mycar.sale << " " << mycar.revenue << endl; 
cout << endl; 

sum = ++mycar.revenue; 

if (mycar.year > 2000) 
{++cnt2000; }

if (mycar.cost < 5000) 
{++cnt5000; }

if (mycar.color == "black")
{ ++cntcolor; }

if (mycar.make == "toyota" && mycar.color == "red") 
{++cntcoma; }
};


cout << "Total Revenue = " << sum << endl; 
cout << "Year greater than 2000 = " << cnt2000 << endl; 
cout << "Cost less than 5000 = " << cnt5000 << endl; 
cout << "Blacks = " << cntcolor << endl; 
cout << "Toyota and Red = " << cntcoma << endl; 


system ("pause"); 
return 0; 

}
Dec 12, 2011 at 8:21pm
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.
Dec 12, 2011 at 9:29pm
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.
Dec 12, 2011 at 10:11pm
Glad to hear it works (^_^)d.

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.

HTH (^_^)/
Last edited on Dec 12, 2011 at 10:13pm
Dec 12, 2011 at 10:36pm
Okay thats makes a lot of sense. Thats why I kept getting 1301. I really do appreciate this. Thank you so much.
Topic archived. No new replies allowed.