[RESOLVED] how can i convert write() procedure to cout? - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 4 FirstFirst 1234 LastLast
Results 31 to 45 of 59

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

  1. #31
    Join Date
    Apr 2009
    Posts
    1,132

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

    Quote Originally Posted by laserlight View Post
    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 11:48 AM.

  2. #32
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    (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 03:12 PM.

  3. #33
    Join Date
    Apr 2009
    Posts
    1,132

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

    Quote Originally Posted by Paul McKenzie View Post
    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

  4. #34
    Join Date
    Apr 2009
    Posts
    1,132

    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

  5. #35
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,865

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

    Quote Originally Posted by Cambalinho View Post
    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 03: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++17 Compiler: Microsoft VS2017 (15.5.0)

  6. #36
    Join Date
    Apr 2009
    Posts
    1,132

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

    Quote Originally Posted by 2kaud View Post
    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)

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

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

    Quote Originally Posted by Cambalinho View Post
    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++17 Compiler: Microsoft VS2017 (15.5.0)

  8. #38
    Join Date
    Apr 2009
    Posts
    1,132

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

    Quote Originally Posted by 2kaud View Post
    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

  9. #39
    Join Date
    Apr 1999
    Posts
    27,449

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

    Quote Originally Posted by Cambalinho View Post
    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

  10. #40
    Join Date
    Apr 2009
    Posts
    1,132

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

    Quote Originally Posted by Paul McKenzie View Post
    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

  11. #41
    Join Date
    Apr 2009
    Posts
    1,132

    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)

  12. #42
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,005

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

    Quote Originally Posted by Cambalinho View Post
    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

  13. #43
    Join Date
    Apr 2009
    Posts
    1,132

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

    Quote Originally Posted by VictorN View Post
    When wil you have learned to use the documentation?
    http://www.cplusplus.com/reference/s...string/string/
    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

  14. #44
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    19,005

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

    Quote Originally Posted by Cambalinho View Post
    ok.. i change these:
    Code:
    string b=(string) argHead;
    to these:
    Code:
    string b=string( argHead);
    Why not just write
    Code:
    string b =  argHead;
    Quote Originally Posted by Cambalinho View Post
    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

  15. #45
    Join Date
    Apr 2009
    Posts
    1,132

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

    Quote Originally Posted by VictorN View Post
    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)
    ?

Page 3 of 4 FirstFirst 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)