user defined manipulators

I am trying to write a code that will accept integers into a vector and then will take a number with more than 3 digits ex. "2431" and then use a class called commas to manipulate into "2,431" by adding the ','. My teacher gave me a power point to look at to help write my commas class but I am beyond confused. My main file is written correctly but I am having trouble w/ the commas class. Any help is appreciated. Also I have been reading more about user defined manipulators through various websites and am still a bit confused on how they function. If anyone knows a link that would further explain what user defined manipulators do / how they function and / or I am lookin for code that I can follow what I am tryin to implement here. Any help / suggestions is appreciated. Thank you.



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

#include <cstdlib>
#include "pa6functions.h"
#include "commas.h"
#include "tertiary.h"
#include "list.h"


int main()
{

        pa6functions::greeting();

        list<unsigned> numbers;
        unsigned p = pa6functions::get_number();
        while (!(p == 0))
        {
           numbers.push(p);
           p = pa6functions::get_number();
        }

        int k = numbers.size();
        list<unsigned> temp;

        for (int i = 0; i < k; ++i)
        {
                temp.push(numbers.top());
                numbers.pop();
        }
        std::cout << "\n\n";

        if (k == 0)
           std::cout << "You didn't enter any positive integers. \n";
        else
        {
           std::cout << std::setw(15) << std::left << "ORIGINAL" ;
           std::cout << std::setw(18) << std::left << "WITH COMMAS" ;
           std::cout << std::left << "IN TERTIARY" << "\n\n";
        }
        for (int i = 0; i < k; ++i)
        {
           std::cout << std::setw(15) << std::left << temp.top();
           std::cout << std::setw(18) << std::left << commas(temp.top());
           std::cout << std::left << tertiary(temp.top()) << "\n";

           temp.pop();
        }
        std::cout << '\n';


        return EXIT_SUCCESS;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
using namespace std;

class commas{
    public:
        commas(std::vector<unsigned> &vec):number(n) { }
        ostream& commas(ostream& out) const
        {
        for(size_t c=0; c<number; c++) out << .\a.;
        return out;
        }

    private:
        size_t number;
};

ostream& operator << (ostream& out, const commas &c)
{
        return c.c_str(out);
}
By "user defined manipuators" do you mean "operator overloading"? That keyword would give you better results in google search. Here is a lesson covering << and >> operators:
http://www.tutorialspoint.com/cplusplus/input_output_operators_overloading.htm

Last edited on
Topic archived. No new replies allowed.