Printing out the contents of a vector

how to print out the contents of the vector? I have stored the contents of a file into a vector that also connects to a class, is it easier to print out a vector or print out the contents of a class? If there is anything else that I could fix, please let me know.Thanks

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
 #include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <iterator>
using namespace std;

class expense
{
public:
	double c_price;
	string c_discription;

	
	string toString()
	{
		stringstream sout;
		sout << "price $(" << c_price << ") discription(" << c_discription << ")";
		return sout.str();
	}
};
int main(int argc, char*argv[])
{
	fstream fin;
	fin.open("expenses2.txt", ios::out);
	if (!fin) 
	{
		cerr << "Error in opening the file" << endl;
		return 1;
	}
	vector<expense> expenses1;
	expense temp;
	while (fin >> temp.c_price >> temp.c_discription) 
	{
	expenses1.push_back(temp);
	}

	string command;
	cout << "spend\nshow\namount >=\nsave file\nhelp\nexit" << endl;
	cin >> command;

	if (command =="show")
	{
		copy(expenses1.begin(), expenses1.end(), ostream_iterator<int>(cout, " "));
	}
	else if (command == "help")
	{
		menu();
	}
	else if (command == "spend")
	{

	}
	cout << argc << "  " << boolalpha << argcCheck(argc) << endl;
	if (argcCheck(argc) == false)
	{
		cout << "invalid command, please try again" << endl;
	}

	return 0;
}
Last edited on
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
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

struct expense
{
    double price ;
    std::string description ;
};

int main()
{
    const std::vector<expense> expense_list { { 3.25, "coffee durian" }, { 6.2, "apple cider" },
                                              { 2.32, "hot cocoa" }, { 2, "malted milk" },
                                              { 1.42, "cinnamon tea" }, { 3.68, "irish coffee" }
                                            };

    // *** print out the contents of the vector ***

    std::cout << "expense list:\n---------------\n"
              << std::fixed << std::setprecision(2) ; // two digits after the decimal point
    double total = 0 ;

    // range based loop: http://www.stroustrup.com/C++11FAQ.html#for
    for( const expense& exp : expense_list ) // for each expense exp in the vector
    {
        // print out the price and description
        std::cout << "USD " << std::setw(5) << exp.price << ' ' << exp.description << '\n' ;
        total += exp.price ; // add price to total
    }

    std::cout << "---------------------------\n" << "USD "
              << std::setw(5) << total << " total expenses\n------------------------\n" ;
}

http://coliru.stacked-crooked.com/a/fe642eb257b1988d
Topic archived. No new replies allowed.