-
August 20th, 2013, 10:42 AM
#31
Re: how can i convert write() procedure to cout?
Originally Posted by laserlight
It depends. I have no clue what is the syntax and semantics of this programming language that you are trying to design and implement. What I do know is that whatever the syntax, you can translate a write to standard output in that language to C++ code involving std::cout, without having to write some C++ function (template, or class) with the C++ syntax that you have in mind. In other words, concentrating on C++ seems like it is taking you off course. Rather, concentrate on the syntax and semantics of your language, and how you will be translating that into C++ without unnecessary frills.
see these:
write("hello world" , NewLine, varname1)
to c++:
'write(' -> 'std::cout <<';
',' - > '<<';
'Newline' -> 'std::endl'
then i can delete the last ')' and add the ';'
(even for Read(varname1) is more or less the same.. except the Read() that must be '_getch()'. the 'cin' don't do the job, i think)
it's easy to translate
Last edited by Cambalinho; August 20th, 2013 at 10:48 AM.
-
August 20th, 2013, 02:09 PM
#32
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
(even for Read(varname1) is more or less the same.. except the Read() that must be '_getch()'. the 'cin' don't do the job, i think)
it's easy to translate
There is no standard C++ function that does this job. The _getch() function may or may not be supported by the compiler.
Maybe you should go over what are the standard functions of C++ before making assumptions about how to translate your functions to C++. So far, your version of "Read" is compiler specific.
If I use a compiler that doesn't have _getch(), or uses a different name than _getch(), then your translations will not work.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 20th, 2013 at 02:12 PM.
-
August 20th, 2013, 02:27 PM
#33
Re: how can i convert write() procedure to cout?
Originally Posted by Paul McKenzie
There is no standard C++ function that does this job. The _getch() function may or may not be supported by the compiler.
Maybe you should go over what are the standard functions of C++ before making assumptions about how to translate your functions to C++. So far, your version of "Read" is compiler specific.
If I use a compiler that doesn't have _getch(), or uses a different name than _getch(), then your translations will not work.
Regards,
Paul McKenzie
unless i can do it with ReadConsole(), but i don't know how
-
August 20th, 2013, 02:32 PM
#34
Re: how can i convert write() procedure to cout?
i try these now:
ReadConsole(GetStdHandle(STD_OUTPUT_HANDLE),NULL,NULL,NULL,NULL);
but don't works
-
August 20th, 2013, 02:52 PM
#35
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
i try these now:
ReadConsole(GetStdHandle(STD _OUTPUT_HANDLE),NULL,NULL,NULL,NULL);
but don't works
STD_OUTPUT_HANDLE for input??????
See
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
Last edited by 2kaud; August 20th, 2013 at 02:57 PM.
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 20th, 2013, 02:54 PM
#36
Re: how can i convert write() procedure to cout?
Originally Posted by 2kaud
STD_OUTPUT_HANDLE for input??????
i never notice that lol
now i get these error:
"First-chance exception at 0x76d0e1c8 in my classs.exe: 0xC0000005: Access violation writing location 0x00000000."
why?
(after press enter, if is another key, is just showed)
-
August 20th, 2013, 03:02 PM
#37
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
i never notice that lol
now i get these error:
"First-chance exception at 0x76d0e1c8 in my classs.exe: 0xC0000005: Access violation writing location 0x00000000."
why?
(after press enter, if is another key, is just showed)
Because you haven't provided a buffer into which to read the characters! Read the documentation.
This will get 1 char from the input buffer
Code:
char ch;
DWORD read;
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL);
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 20th, 2013, 03:10 PM
#38
Re: how can i convert write() procedure to cout?
Originally Posted by 2kaud
Because you haven't provided a buffer into which to read the characters! Read the documentation.
This will get 1 char from the input buffer
Code:
char ch;
DWORD read;
ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &ch, 1, &read, NULL);
thanks for all
-
August 20th, 2013, 04:17 PM
#39
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
i try these now:
ReadConsole(GetStdHandle(STD_OUTPUT_HANDLE),NULL,NULL,NULL,NULL);
but don't works
That is a Windows API specific function, and this forum is geared towards standard C++, not Windows API functions. There is no "ReadConsole" function in standard C++.
If you're asking about Windows API functions, then there is a WinAPI forum to ask these questions.
Regards,
Paul McKenzie
-
August 20th, 2013, 04:50 PM
#40
Re: how can i convert write() procedure to cout?
Originally Posted by Paul McKenzie
That is a Windows API specific function, and this forum is geared towards standard C++, not Windows API functions. There is no "ReadConsole" function in standard C++.
If you're asking about Windows API functions, then there is a WinAPI forum to ask these questions.
Regards,
Paul McKenzie
sorry about that, but the normal C++ don't have it. that's why you finished these topic with 1 API function. sorry about that
-
September 10th, 2013, 10:54 AM
#41
Re: how can i convert write() procedure to cout?
Code:
void write()
{
cout <<"";
}
template <typename A, typename ...B>
void write(A argHead, B... argTail)
{
cout << argHead;
write(argTail...);
}
template <typename A, typename ...B>
void write(char *argHead, B... argTail2)
{
string b=(string) argHead;
cout << b;
write(argTail2...);
}
is possible convert the char* type to string?
(my objective is accept the '+' for concat the strings)
-
September 10th, 2013, 11:12 AM
#42
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
is possible convert the char* type to string?
When wil you have learned to use the documentation?
http://www.cplusplus.com/reference/s...string/string/
Victor Nijegorodov
-
September 10th, 2013, 11:29 AM
#43
Re: how can i convert write() procedure to cout?
Originally Posted by VictorN
ok.. i change these:
Code:
string b=(string) argHead;
to these:
Code:
string b=string( argHead);
but when i use it:
Code:
Console b;
b.write("hello " + "world" );
i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|10|error: invalid operands of types 'const char [7]' and 'const char [6]' to binary 'operator+'|"
what i need is that accept that line
-
September 10th, 2013, 11:35 AM
#44
Re: how can i convert write() procedure to cout?
Originally Posted by Cambalinho
ok.. i change these:
Code:
string b=(string) argHead;
to these:
Code:
string b=string( argHead);
Why not just write
Code:
string b = argHead;
Originally Posted by Cambalinho
but when i use it:
Code:
Console b;
b.write("hello " + "world" );
i get these error:
"C:\Users\Joaquim\Documents\CodeBlocks\My Class\main.cpp|10|error: invalid operands of types 'const char [7]' and 'const char [6]' to binary 'operator+'|"
what i need is that accept that line[/QUOTE]This line has nothig to do with your previous question. Nor has it anything to do with the std::string class!
You are tryinf to add two pointers which is wrong. Is you want to concatenate thses strings - use strcat function.
Victor Nijegorodov
-
September 10th, 2013, 11:39 AM
#45
Re: how can i convert write() procedure to cout?
Originally Posted by VictorN
Why not just write
Code:
string b = argHead;
what i need is that accept that line
This line has nothig to do with your previous question. Nor has it anything to do with the std::string class!
You are tryinf to add two pointers which is wrong. Is you want to concatenate thses strings - use strcat function.[/QUOTE]
let me ask 1 thing:
if i can do these:
Code:
b.write((string)"hello " + "world" );
why i can't do these:
Code:
template <typename A, typename ...B>
void write(char *argHead, B... argTail2)
{
cout <<(string) argHead;
write(argTail2...);
}
(the error stills be when i use these function)
?
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
|