triumphost 120 Posting Whiz

I'm trying to write a cout formatter that uses the comma operator and insertion operator. I wasn't able to get that to work so I decided to write it similar to std::boolalpha.

when you use std::boolalpha, it is able to change 0 values to true and false and you can use it like cout << boolalpha.

I was trying to do the same with my "fout" which is supposed to format things printf style by doing:

std::cout<<"Some normal text here"<< fout <<"Test %"<< 1;

and that would print "Some normal text here Test 1".

but that's a bad idea because I wouldn't be able to enter anything else :l

So instead I decided it'd be best if I used the comma operator instead and do:

std::cout<<"Some normal text here" << fout <<"Test %, %", 1, 2<< "works 100% fine."<<std::endl

such that it'd print "Some normal text here Test 1, 2 works 100% fine."

But I'm stuck. I cannot seem to figure out how to get this behaviour at all. I wrote fcout which does this through a function:

template<typename... Args>
void fcout(const char* S, Args... args)
{
    std::size_t I = 0;
    std::vector<tstring> Arguments;
    //Unpack args into Arguments..

    while(*S)
    {
        if (*S == '%')
        {
            if (Arguments.size() <= I)
                throw std::logic_error("Error: Invalid Format. Missing Arguments.");

            cout<<Arguments[I++];
        }
        else
        {
            cout<<*S;
        }
        ++S;
    }
}

BUT its actually bothering me not knowing how to do exactly what I want. Similar to:

#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>


std::ostream& fout (std::ostream& I)
{
    //Some how format my text here using fcout or something similar.. Then return it as the ostream.
    return I;
}

int main(int argc, char* argv[])
{    
    std::cout<< fout << "Test %", 1 << "Other stuff 20%.";
}

Is there a way to do what I want or do I have to stick with what I already have?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.