CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 4 1234 LastLast
Results 1 to 15 of 59
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [RESOLVED] how can i convert write() procedure to cout?

    i'm building a Console class(i realy need it). and i don't understand how can i build the Write() procedure
    heres the struture:

    write(varname1[,varnamex])

    how can i convert it to cout?

  2. #2
    Join Date
    Mar 2005
    Posts
    55

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho View Post
    i'm building a Console class(i realy need it). and i don't understand how can i build the Write() procedure
    heres the struture:

    write(varname1[,varnamex])

    how can i convert it to cout?
    Not following what you mean. Can you clarify it more?

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by manojg View Post
    Not following what you mean. Can you clarify it more?
    sample:

    Code:
    write( varname1, varname2, newline,...)
    something like these. if i can't use the ',', then i want use the '+' or '&'
    but i don't understand how can i combine these with cout

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho View Post
    sample:

    Code:
    write( varname1, varname2, newline,...)
    something like these. if i can't use the ',', then i want use the '+' or '&'
    but i don't understand how can i combine these with cout
    Code:
    cout << varname1 << varname2 << endl;
    I'm still not sure I'm understanding what you mean?

    Are you wanting a function with a variable number of arguments? Have a look at
    http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx

    Also have a look at
    http://www.cplusplus.com/reference/cstdio/vsprintf/

    which describes vsprintf which creates a char array from a variable number of parameters.

    It might be useful if you could provide some actual code example of what you are trying to achieve.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by 2kaud View Post
    Code:
    cout << varname1 << varname2 << endl;
    I'm still not sure I'm understanding what you mean?

    Are you wanting a function with a variable number of arguments? Have a look at
    http://msdn.microsoft.com/en-us/libr...=VS.71%29.aspx

    Also have a look at
    http://www.cplusplus.com/reference/cstdio/vsprintf/

    which describes vsprintf which creates a char array from a variable number of parameters.

    It might be useful if you could provide some actual code example of what you are trying to achieve.
    heres the code that i have for now:

    Code:
    #define NewLine endl
    void Write( anytype varname1,...) //any type	{
    		int i;
    		for (i=0; i<NumberOfArguments; i++)
    		{
    			cout << ArgumentList[i];
    		}
    	}
    the code isn't complete, but i have 2 problems:
    1 - what type i can use(for use any type);
    2 - how i know the number of arguments?(i think that you have in that links... i must see better)

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Code:
    void Write( anytype varname1,...) //any type
    	{
    		int i;
    		int num;
    		va_list NumberOfArguments;
    		va_start ( NumberOfArguments, num );
    		for (i=0; i<num; i++)
    		{
    			cout << va_arg ( enum, anytype ); //show the variable
    		}
    		va_end (NumberOfArguments);
    	}
    can you advice me more?

  7. #7
    Join Date
    Oct 2005
    Location
    Bangalore
    Posts
    167

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho View Post
    Code:
    void Write( anytype varname1,...) //any type
        {
            int i;
            int num;
            va_list NumberOfArguments;
            va_start ( NumberOfArguments, num );
            for (i=0; i<num; i++)
            {
                cout << va_arg ( enum, anytype ); //show the variable
            }
            va_end (NumberOfArguments);
        }
    can you advice me more?
    I think, You are looking for variable number of arguments. Go through the below post

    http://stackoverflow.com/questions/1...arguments-in-c

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: how can i convert write() procedure to cout?

    Before reaching for variable argument lists, look at the syntax for formatted output commonly used with output streams such as std::cout. The basic idea here is to overload operator<< such that a reference to the output stream is the left hand parameter and the object to be printed is the right hand parameter. Then, these calls are chained together by returning a reference to the same output stream.

    You can do something similiar for your Console class' Write function, e.g., a user of the class might chain Write calls to write objects of different types to the console in sequence. Exactly how depends on what exactly you want to do and with what syntax.

    The "convert it to cout" part is too vague: are you talking about implementing your Write function to write to std::cout?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by laserlight View Post
    Before reaching for variable argument lists, look at the syntax for formatted output commonly used with output streams such as std::cout. The basic idea here is to overload operator<< such that a reference to the output stream is the left hand parameter and the object to be printed is the right hand parameter. Then, these calls are chained together by returning a reference to the same output stream.

    You can do something similiar for your Console class' Write function, e.g., a user of the class might chain Write calls to write objects of different types to the console in sequence. Exactly how depends on what exactly you want to do and with what syntax.

    The "convert it to cout" part is too vague: are you talking about implementing your Write function to write to std::cout?
    yes. because the prinf() is more complicated and i want be more simple
    intead use '<<', my function uses ',' and '(',')' for open and close the function
    but i don't understand how can i do a variable with any type
    bagavathikumar:

    now i did these code:
    Code:
    template<typename T, typename... Args>	void Write(T t, Args... args) // recursive variadic function
    	{
    		cout << t;		
    		Write(args...) ;
    	}
    like you see, i'm using any type
    (seems that site isn't comptible with VS2010)
    but i get now 4 errors:

    1 - "error C2143: syntax error : missing ',' before '...'";
    2 - "error C2061: syntax error : identifier 'Args'";
    3 - "error C2783: 'void Console::Write(T)' : could not deduce template argument for '__formal'";
    4 - "IntelliSense: no instance of function template "Console::Write" matches the argument list";
    any advice?

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by Cambalinho
    yes. because the prinf() is more complicated and i want be more simple
    Where does prinf (printf?) come into the picture? You want to come up with something more simple than what? It almost sounds as if you think std::cout is used together with printf.

    Quote Originally Posted by Cambalinho
    intead use '<<', my function uses ',' and '(',')' for open and close the function
    Looking at your example in post #3, you effectively want variable length argument list syntax. Unfortunately, if I remember correctly, the variable length argument list feature does not cater to objects of class types that do not satisfy certain requirements.

    Furthermore, if you are not adding anything over the standard conventions of output streams in C++, why bother? It sounds as if you are merely planning to change the syntax, and that is a Bad Thing because it means your library users have to learn yet another thing with no value added. The only thing you have going is the vague notion of something "more simple", but sorry this:
    Code:
    write( varname1, varname2, newline,...)
    is not "more simple" than:
    Code:
    std::cout << varname1 << varname2 << '\n';
    If you want to take a look a project that actually does add value, read up on Boost.Format.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  11. #11
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by laserlight View Post
    Where does prinf (printf?) come into the picture? You want to come up with something more simple than what? It almost sounds as if you think std::cout is used together with printf.


    Looking at your example in post #3, you effectively want variable length argument list syntax. Unfortunately, if I remember correctly, the variable length argument list feature does not cater to objects of class types that do not satisfy certain requirements.

    Furthermore, if you are not adding anything over the standard conventions of output streams in C++, why bother? It sounds as if you are merely planning to change the syntax, and that is a Bad Thing because it means your library users have to learn yet another thing with no value added. The only thing you have going is the vague notion of something "more simple", but sorry this:
    Code:
    write( varname1, varname2, newline,...)
    is not "more simple" than:
    Code:
    std::cout << varname1 << varname2 << '\n';
    If you want to take a look a project that actually does add value, read up on Boost.Format.
    ok... but i need build that function
    (i'm building the console class for my new language... that's why i need 'rebuild' the cout)
    can you advice me for correct that errors?
    but can i create the 'cout'(with write keyword) in a class?
    Last edited by Cambalinho; August 19th, 2013 at 04:53 AM.

  12. #12
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how can i convert write() procedure to cout?

    Using functions with variable number of arguments via the va_ set of macros is one of the more dusky corners of c++ inherited from c. I would strongly advise against using this mechanism if the result can be implemented some other way (eg function or operator overloading).

    If you are using c++11 and really, really want functions with variable number of arguments, the answer is probably to use Variadic Templates.

    http://www.open-std.org/jtc1/sc22/wg...2006/n2087.pdf
    http://www.stroustrup.com/C++11FAQ.h...adic-templates

    But before you get bogged down in the detail of implementing these templates, think again if you really need this functinality and that what you want to do couldn't be done some other simpler way.
    Last edited by 2kaud; August 19th, 2013 at 08:39 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  13. #13
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    Quote Originally Posted by 2kaud View Post
    Using functions with variable number of arguments via the va_ set of macros is one of the more dusky corners of c++ inherited from c. I would strongly advise against using this mechanism if the result can be implemented some other way (eg function or operator overloading).

    If you are using c++11 and really, really want functions with variable number of arguments, the answer is probably to use Variadic Templates.

    http://www.open-std.org/jtc1/sc22/wg...2006/n2087.pdf
    http://www.stroustrup.com/C++11FAQ.h...adic-templates

    But before you get bogged down in the detail of implementing these templates, think again if you really need this functinality and that what you want to do couldn't be done some other simpler way.
    that tutorial is limited, the 1st argument is the master and tell us, indirectly, how many arguments received
    i need use the write() like these:
    write(var1, var2, "hello world")
    the order i don't care, but i don't use strings or arguments for tell me the number of arguments.

  14. #14
    Join Date
    Apr 2009
    Posts
    1,355

    Re: how can i convert write() procedure to cout?

    note: can i use C++11 in VS2010?

    the overloading was the best thing that i found:
    Code:
    //Write
    	//1 argument
    	template<typename Type1>
    		void Write(Type1 var1) { cout << var1; }
    	//2 arguments
    	template<typename Type1, typename Type2>
    		void Write(Type1 var1, Type2 var2) { cout << var1 << var2; }
    	//3 arguments
    	template<typename Type1, typename Type2, typename Type3>
    		void Write(Type1 var1, Type2 var2, Type3 var3) { cout << var1 << var2 << var3; }
    	//4 arguments
    	template<typename Type1, typename Type2, typename Type3, typename Type4>
    		void Write(Type1 var1, Type2 var2, Type3 var3, Type4 var4) { cout << var1 << var2 << var3 << var4; }
    
    	//Read
    	//empty
    	void Read(){_getch();}
    	//1 argument
    	template<typename Type1>
    		void Read(Type1 &var1) { cin >> var1; }
    	//2 arguments
    	template<typename Type1, typename Type2>
    		void Read(Type1 &var1, Type2 &var2) { cin >> var1 >> var2; }
    	//3 arguments
    	template<typename Type1, typename Type2, typename Type3>
    		void Read(Type1 &var1, Type2 &var2, Type3 &var3) { cin >> var1 >> var2 >> var3; }
    	//4 arguments
    	template<typename Type1, typename Type2, typename Type3, typename Type4>
    		void Read(Type1 &var1, Type2 &var2, Type3 &var3, Type4 &var4) { cin >> var1 >> var2 >> var3 >> var4; }
    thanks for all
    i accept more advices

  15. #15
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: how can i convert write() procedure to cout?

    Try something like this. Note that variadic templates in c++11 are not yet implemented in MSVS2010 or 2012.

    Code:
    #include <iostream>
    using namespace std;
    
    void write()
    {
       cout << " ";
    }
    
    template <typename A, typename ...B>
    void write(A argHead, B... argTail)
    {
       cout << argHead << " ";
       write(argTail...);
    }
    
    
    int main(void)
    {
       write(1, 2, "qwerty", 4.45, "five", 6);
       return 0;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 4 1234 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured