How to fix this coding ?

i have a coding that need to read from a file. i have to find the sum of 2 column from the file. the file is like this :

Mon 12 5 50
Tue 20 8 13
Wed 20 2 20
Thu 10 4 33

i want to find the sum of column 2 with column 3 and determine for each day either the expenses increase or decrease
i want the output to be : Monday , the expenses are rm 17.
i have wrote the coding but the loop for no change still appear even it should be decrease

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int expenses (ifstream& nameFile);


int main()
{
	string data;
	ifstream nameFile("money_data.txt");

	if (!nameFile) 
		return (cout << " ERROR : cannot open file.\n"), 1;

    expenses(nameFile);

}
void expenses (ifstream& nameFile)
{

    nameFile.clear();
    nameFile.seekg(0);

    double sum ;

    for (double DAY {}, FOOD {}, TRANSPORT {}, SHOPPING {};
           nameFile >> DAY >> FOOD >> TRANSPORT >> SHOPPING; )
        {

          total = FOOD + TRANSPORT ;
          double current_sum;

          cout << " Day :" << DAY << "   Total expenses : RM" << sum;

                if( current_sum > sum)
                   {cout << " (decrease)" << endl;}
                if( current_sum < sum )
                  {cout << " (increase)" << endl;}
                if( current_sum = sum )
                    cout << " (no change)" << endl; ;

               sum = current_sum ;

            }
}


The output should be

Mon Total expenses : RM17
Tue Total expenses : RM28 (increase)
Wed Total expenses : RM22 (decrease)
Thu Total expenses : RM14 (decrease )

can anyone help me ? Thank you in advance
Last edited on
You are not doing anything with current_sum or sum. They remain uninitialized. Something like this perhaps?
1
2
3
4
5
6
7
8
    double sum = 0;

    for (double DAY {}, FOOD {}, TRANSPORT {}, SHOPPING {};
           nameFile >> DAY >> FOOD >> TRANSPORT >> SHOPPING; )
        {

          total = FOOD + TRANSPORT ;
          double current_sum = sum + total ;
1st column (day) is a string, not a number!

Perhaps:

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 <fstream>
#include <string>

void expenses(std::istream& nameFile);

int main()
{
	std::ifstream nameFile("money_data.txt");

	if (!nameFile)
		return (std::cout << " ERROR : cannot open file.\n"), 1;

	expenses(nameFile);
}

void expenses(std::istream& nameFile)
{
	nameFile.clear();
	nameFile.seekg(0);

	double prev_sum {-1.0};
	std::string day;

	for (double food {}, transport {}, shopping {};
		nameFile >> day >> food >> transport >> shopping; ) {
		const auto day_sum {food + transport};

		std::cout << " Day :" << day << "   Total expenses : RM" << day_sum;

		if (prev_sum >= 0)
			if (day_sum > prev_sum)
				std::cout << " (increase)";
			else if (day_sum < prev_sum)
				std::cout << " (decrease)";
			else
				std::cout << " (no change)";

		std::cout << '\n';
		prev_sum = day_sum;
	}
}



 Day :Mon   Total expenses : RM17
 Day :Tue   Total expenses : RM28 (increase)
 Day :Wed   Total expenses : RM22 (decrease)
 Day :Thu   Total expenses : RM14 (decrease)


alright noted but why at line 22 prev_sum must be minus with 1.0 ?
that is an initialization, not a subtraction. its set == to -1
this helps the logic at line 31 and onward.
Last edited on
Topic archived. No new replies allowed.