Please help with for loop

This program should add all expenses and subtract them from the budget.
However, it's not working!
I've been working on it for hours, please help.
What am I missing? :(


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
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
 
int main() 
{	
 
//INPUT VARIABLES
float budget;		
float expenses;		
float total;			
int count; 
 
expenses = 0.0;
budget = 0.0;
 
//Input budget money
cout << "Enter the amount of money you budgeted: " << endl;
cin >> budget;
 
//Input expenses
for (count = 1; count <= 6; count++)
{
cout << "Enter all of your expenses for the month: " << endl;
cin >> expenses;
expenses += expenses;
}
 
//Processing the total money after expenses
total = budget - expenses;
 
//Output the total money after expenses
cout << setprecision(2) << fixed;
cout << "Your total budget after expenses: $" << total << endl;
if (total > expenses)
cout << "Your expenses are under the budget!";
else
cout << "Your expenses are over the budget!";

return 0;
}
I guess total is the remaining money, so you need the check if the remaining money is >= 0.
Another option is just to check if expenses are <= budget.
@ OP,

To give you an idea...
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{

    //INPUT VARIABLES
    float budget = 0.0;
    float expenses = 0.0;
    float total = 0.0;
    float runningExpenses = 0.0; //Add a variable to maintain a running total


    //Input budget money
    cout << "Enter the amount of money you budgeted: " << endl;
    cin >> budget;

    //Input expenses
    for (int count = 1; count <= 6; count++)
    {
	   cout << "Enter all of your expenses for the month: " << endl;
	   cin >> expenses;
	   //expenses += expenses; it just double the value on the variable expenses and not running total.
	   runningExpenses += expenses; 
    }

    //Processing the total money after expenses
    //total = budget - expenses; 
    total = budget - runningExpenses;

    //Output the total money after expenses
    cout << setprecision(2) << fixed;
    cout << "Your total budget after expenses: $" << total << endl;
    if (total > runningExpenses) (budget > runningExpenses) // I gave wrong info (total > runningExpenses).
	   cout << "Your expenses are under the budget!";
    else
	   cout << "Your expenses are over the budget!";

    return 0;
}


Now, something to think about. If the budget is 100 and the total expenses are 100, are the expenses under or over the budget? No need to answer to me. I am just giving you a pointer on the if statements.
Last edited on
@ chicofeo,

runningExpenses is where the value of expenses is stored after each loop?



Can you help me figure out a way to have it say "You're breaking even this month!" if the expenses and budget are equal?

Yes on the purpose of the running expense variable.

Pseudocode
if total equals running expenses, then the expenses and the budget is equal. I gave wrong info. Sorry about that.

if budget equals running expenses, then the expenses and the budget is equal.
Last edited on
Please someone take a look at my code. I can't get it to work :(


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{

//INPUT VARIABLES
float budget = 0.0;
float expenses = 0.0;
float total = 0.0;
float runningExpenses = 0.0; //Add a variable to maintain a running total


//Input budget money
cout << "Enter the amount of money you budgeted: " << endl;
cin >> budget;

//Input expenses
for (int count = 1; count <= 6; count++)
{
float expenses;
cout << "Enter all of your expenses for the month: " << endl;
cin >> expenses;
runningExpenses += expenses;
}

total = budget - runningExpenses;
cout << setprecision(2) << fixed;
cout << "Your total budget after expenses: $" << total << endl;


if (total > runningExpenses)
{
cout << "Your expenses are less than your budget!\n";
}

if (total < runningExpenses)
{
cout << "Your expenses are more than your budget!\n";
}

if (total == runningExpenses)
{
cout << "You're breaking even between your expenses and budget!\n";
}


return 0;
}





I truly apologize for giving the wrong info. Since I gave you wrong info, Below is the code, which should work.

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{

    //INPUT VARIABLES
    float budget = 0.0;
    float expenses = 0.0;
    float total = 0.0;
    float runningExpenses = 0.0; //Add a variable to maintain a running total


    //Input budget money
    cout << "Enter the amount of money you budgeted: " << endl;
    cin >> budget;

    //Input expenses
    for (int count = 1; count <= 6; count++)
    {
	   cout << "Enter all of your expenses for the month: " << endl;
	   cin >> expenses;
	   runningExpenses += expenses;
    }

    //Processing the total money after expenses
    total = budget - runningExpenses;

    //Output the total money after expenses
    cout << setprecision(2) << fixed;
    cout << "Your total budget after expenses: $" << total << endl;
    if (budget > runningExpenses)
	   cout << "Your expenses are under the budget!";
    else if (budget < runningExpenses)
	   cout << "Your expenses are over the budget!";
    else 
	   cout << "You're breaking even between your expenses and budget!\n";

    return 0;
}
@ chicofeo,

No apologies needed. You're trying to help and I thank you for it very much.

With your last post I learned about "else if" statements. The book wasn't being much help.

Wow now it all makes so much sense and seems so easy that I'm embarrassed I didn't see it before haha.

I was using the wrong variable in the if statements.
I see now that the total variable is only for the output of the total expenses.
Topic archived. No new replies allowed.