[HELP PLEASE]

How do I code this. What is the proper coding for this problem? Thanks in advance :)

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
Read and format a list of amounts in a file named AMOUNT.TXT. 
It should appear right justified and must have exactly two digits after the decimal point.
Say the number is 12, it should appear as 12.00. 
If the number is 1.5, it should appear as 1.50.
Furthermore, your program must display the total sum of all the amounts read.

Note: Amount will not exceed 1,000,000 pesos.

SAMPLE INPUT FROM FILE
1
2
5.5
10
1100
100.5

SAMPLE OUTPUT ON SCREEN:
0001.00
0002.00
0005.50
0010.50
1100.00
0100.50
=======
1219.50
What have you written so far?

Also, here's a hint:
http://www.cplusplus.com/reference/iomanip/setprecision/
This one:

Removed due to some personal reasons
Last edited on
What problem are you having?
Aligning and having two decimal points each number.
Thanks alot. Im done :))
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
#include <fstream>
#include <string>
#include <vector>
#include <sstream>

using namespace std;

const char* filename = "c:\\temp\\AMOUNT.txt";

void readInput(const char* input, vector<double>& v);
void writeOutput(vector<double>& v);

int main()
{
	vector<double> v;
	readInput(filename, v);
	writeOutput(v);
  
	system("pause");
	return 0;
}

void readInput(const char* input, vector<double>& v)
{
	ifstream f;
	f.open(input, ifstream::in);
	if (f.is_open())
	{
		string line;
		while (getline(f, line))
		{
			stringstream ss(line);
			double d;
			ss >> d;
			v.push_back(d);
		}
		f.close();
	}
}

void writeOutput(vector<double>& v)
{
	double total(0.0);
	for (auto p = v.begin(); p != v.end(); ++p)
	{
		printf("%07.2f\n", *p);
		total += *p;
	}
	printf("=======\n");
	printf("%07.2f\n", total);
}
This doesn't look like your previous program, and you didn't even use my suggestions.

This looks suspiciously like someone else wrote this program for you, what with advanced concepts like vectors, references, use of the auto keyword which to my knowledge is not taught in any classes yet.

You will not learn if you take code from others and turn it in as your own.
Topic archived. No new replies allowed.