If Statement containing math

Can an if statement contain a math function?


For example:
1
2
3
4
5
6
7
8
9
cout << endl << "Utilities: " << "$ "<< user.utilities << endl;
	if (x > y)
	{
		cout << "Your x was over the budget\n";
	}
	else
	{
		cout << "Your x was under the budget\n";
	}


Can I go into the if statement and modify it so that the cout also provides a numerical value? Can I somehow show in an equation from the if statement how much x exceeds y? (or vice-versa)
closed account (Dy7SLyTq)
i guess so, but i think you are more looking for the terenary operator. it is used as:
(condition) ? (if condition is true) : (if condition is false);

so how you would use it here:
 
cout << endl << "Utilities: " << "$ "<< user.utilities << endl << (x > y) ? "Your x was over the budget\n" : "Your x was under the budget\n";
Can then the terenary operator have a function in it? I want the equation to show how much x exceeds y, (or how much x is less than y). In other words, I would like the operator to show the results, not just give the word report.
closed account (Dy7SLyTq)
yeah sure
I would need to define the equation beforehand? Or can I just literally put it in the operator? I tried putting in

by value (total.housing)

where:

total.housing = user.housing - fixed.housing; Defined in a prior void equation, but got the error "Member Reference Type base "double", is not a structure or a union."
Last edited on
closed account (Dy7SLyTq)
your not being very clear in your posts. could you please post your code?
Any function can be implicitly translated to an expression.
Any expression can be implicitly translated to a boolean value.
Conditional control structures make use of boolean values.
Last edited on
Here is the code:
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
using namespace std;

struct MonthlyBudget
{
	double housing,
    utilities,
    householdExpenses,
    transportation,
    food,
    medical,
    insurance,
    entertainment,
    clothing,
    miscellaneous;
    
	MonthlyBudget(double h= 0, double u= 0, double he = 0, double t= 0, double f= 0, double m= 0, double i= 0, double e= 0, double c= 0, double misc= 0)
	{
		housing = h;
		utilities = u;
		householdExpenses = he;
		transportation = t;
		food = f;
		medical = m;
		insurance = i;
		entertainment = e;
		clothing = c;
		miscellaneous = misc;
	}
    
};

void equations()
{
    double total;
    double user;
    double fixed;
    
    total.housing = user.housing - fixed.housing;
}


void getAmount(MonthlyBudget &);
void displayAmount(MonthlyBudget &, MonthlyBudget &);

int main()
{
	MonthlyBudget fixed(500.00, 150.00, 65.00, 50.00, 250.00, 30.00, 100.00, 150.00, 75.00, 50.00);
	MonthlyBudget user;
    
	getAmount(user);
	displayAmount(user, fixed);
    
	return 0;
}

void getAmount(MonthlyBudget &user)
{
	cout << endl << "Please enter your monthly expenses:\n";
	cout  << endl << "Housing: ";
	cin >> user.housing;
	cout << endl << "Utilities: ";
	cin >> user.utilities;
	cout << endl << "Household Expenses: ";
	cin >> user.householdExpenses;
	cout << endl << "Transportation: ";
	cin >> user.transportation;
	cout << endl << "Food: ";
	cin >> user.food;
	cout << endl << "Medical: ";
	cin >> user.medical;
	cout << endl << "Insurance: ";
	cin >> user.insurance;
	cout << endl << "Entertainment: ";
	cin >> user.entertainment;
	cout << endl << "Clothing: ";
	cin >> user.clothing;
	cout << endl << "Miscellaneous: ";
	cin >> user.miscellaneous;
}

void displayAmount(MonthlyBudget &user, MonthlyBudget &fixed)
{
	cout << endl << "Here are your individual totals for the month:\n";
	cout << endl <<  "Housing: " << "$ " << user.housing << endl;
	if (user.housing > fixed.housing)
	{
		cout << "Your housing expenses was over the budget, by value (total.housing)\n";
	}
	else
	{
		cout << "Your housing expenses was under the budget\n";
	}
    
	cout << endl << "Utilities: " << "$ "<< user.utilities << endl;
	if (user.utilities > fixed.utilities)
	{
		cout << "Your utilities expenses was over the budget\n";
	}
	else
	{
		cout << "Your utilities expenses was under the budget\n";
	}
    
	cout << endl << "Household Expenses: " << "$ " << user.householdExpenses << endl;
	if (user.householdExpenses > fixed.householdExpenses)
	{
		cout << "Your household expenses was over the budget\n";
	}
	else
	{
		cout << "Your household expenses was under the budget\n";
	}
    
	cout << endl << "Transportation: " << "$ " << user.transportation << endl;
	if (user.transportation > fixed.transportation)
	{
		cout << "Your transportation expenses was over the budget\n";
	}
	else
	{
		cout << "Your transportation expenses was under the budget\n";
	}
    
	cout << endl << "Food: " << "$ " << user.food << endl;
	if (user.food > fixed.food)
	{
		cout << "Your food expenses was over the budget\n";
	}
	else
	{
		cout << "Your food expenses was under the budget\n";
	}
    
	cout << endl << "Medical:: " << "$ " << user.medical << endl;
	if(user.medical > fixed.medical)
	{
		cout << "Your medical expenses was over the budget\n";
	}
	else
	{
		cout << "Your medical expenses was under the budget\n";
	}
    
	cout << endl << "Insurance: " << "$ " << user.insurance << endl;
	if (user.insurance > fixed.insurance)
	{
		cout << "Your insurance expenses was over the budget\n";
	}
	else
	{
		cout << "Your insurance expenses was under the budget\n";
	}
    
	cout << endl << "Entertainment: " << "$ " << user.entertainment << endl;
	if (user.entertainment > fixed.entertainment)
	{
		cout << "Your entertainment expenses was over the budget\n";
	}
	else
	{
		cout << "Your entertainment expenses was under the budget\n";
	}
    
	cout << endl << "Clothing: " << "$ " << user.clothing << endl;
	if (user.clothing > fixed.clothing)
	{
		cout << "Your clothing expenses was over the budget\n";
	}
	else
	{
		cout << "Your clothing expenses was under the budget\n";
	}
    
	cout << endl << "Miscellaneous: " << "$ " << user.miscellaneous << endl;
	if (user.miscellaneous > fixed.miscellaneous)
	{
		cout << "Your miscellaneous expensees was over the budget\n";
	}
	else
	{
		cout << "Your miscellaneous expenses was under the budget\n";
	}
    
	double userBudget = user.housing + user.utilities + user.householdExpenses + user.transportation + user.food + user.medical + user.insurance + user.entertainment + user.clothing + user.miscellaneous;
	double fixedBudget = fixed.housing + fixed.utilities + fixed.householdExpenses + fixed.transportation + fixed.food + fixed.medical + fixed.insurance + fixed.entertainment + fixed.clothing + fixed.miscellaneous;
    
	cout << endl << "Total expenses for the month: $ " << userBudget;
    
	if(userBudget > fixedBudget)
	{
		cout << endl << "Your expenses exceed your monthly average budget\n";
	}
	else
	{
		cout << endl << "Your expenses are under your monthly average budget\n";
	}
}


I want the if/else statements to show values, not just give a yes or no answer.
I would use a loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>
#include <cmath>

int main(int argc, char* argv[]) {

	const short NUM_ITEMS = 10;

	const char* items[NUM_ITEMS]	= {"housing", "utilities", "household", "transportation", "food", "medical", "insurace", "entertainment", "clothing", "miscellaneous"};
	const double fixed[NUM_ITEMS]	= {500.00, 150.00, 65.00, 50.00, 250.00, 30.00, 100.0, 150.00, 75.00, 50.00};
	double expenses[NUM_ITEMS]	= {100.00, 200.00, 100.00, 200.00, 100.00, 200.00, 100.00, 200.00, 100.00, 200.00};//possible expenses

	for(unsigned short i=0; i<NUM_ITEMS; ++i) {
		std::cout << "Your " << items[i] << " expenses were " << (expenses[i]>fixed[i]?"over":"under") << " the budget by $" << std::fixed << std::setprecision(2) << abs(fixed[i]-expenses[i]) << std::endl;
	}

	std::cin.get();
	return 0;
}


I've elected to make matching fixed costs and expenses qualify as "under" the budget.
Last edited on
I think I am going to bang my head repeatedly. I figured out what I was supposed to do:

Instead of this:
cout << "Your housing expenses was over the budget\n";

I did this:
cout << "Your housing expenses was over the budget, by value: " <<user.housing - fixed.housing <<"\n";

And I found the specifications I needed.
Topic archived. No new replies allowed.