-
August 18th, 2013, 08:36 AM
#1
[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![Frown](images/smilies/frown.gif)
heres the struture:
write(varname1[,varnamex])
how can i convert it to cout?
-
August 18th, 2013, 10:19 AM
#2
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by Cambalinho
i'm building a Console class(i realy need it). and i don't understand how can i build the Write() procedure ![Frown](images/smilies/frown.gif)
heres the struture:
write(varname1[,varnamex])
how can i convert it to cout?
Not following what you mean. Can you clarify it more?
-
August 18th, 2013, 10:30 AM
#3
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by manojg
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 '&'![Wink](images/smilies/wink.gif)
but i don't understand how can i combine these with cout
-
August 18th, 2013, 10:54 AM
#4
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by Cambalinho
sample:
Code:
write( varname1, varname2, newline,...)
something like these. if i can't use the ',', then i want use the '+' or '&' ![Wink](images/smilies/wink.gif)
but i don't understand how can i combine these with cout ![Frown](images/smilies/frown.gif)
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)
-
August 18th, 2013, 01:20 PM
#5
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by 2kaud
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 )
-
August 18th, 2013, 01:27 PM
#6
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?
-
August 19th, 2013, 01:36 AM
#7
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by Cambalinho
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
-
August 19th, 2013, 02:31 AM
#8
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?
-
August 19th, 2013, 04:06 AM
#9
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by laserlight
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![Wink](images/smilies/wink.gif)
intead use '<<', my function uses ',' and '(',')' for open and close the function![Wink](images/smilies/wink.gif)
but i don't understand how can i do a variable with any type![Frown](images/smilies/frown.gif)
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![Wink](images/smilies/wink.gif)
(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?
-
August 19th, 2013, 04:39 AM
#10
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) 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](images/misc/quote_icon.png) 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.
-
August 19th, 2013, 04:45 AM
#11
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by laserlight
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![Wink](images/smilies/wink.gif)
(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.
-
August 19th, 2013, 08:29 AM
#12
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)
-
August 19th, 2013, 08:47 AM
#13
Re: how can i convert write() procedure to cout?
![Quote](images/misc/quote_icon.png) Originally Posted by 2kaud
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![Frown](images/smilies/frown.gif)
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.
-
August 19th, 2013, 10:47 AM
#14
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
-
August 19th, 2013, 11:24 AM
#15
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. ![Cry](images/smilies/cry.gif)
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|