page 293 of C++ primer,4th edition

Discussion in 'C++' started by asdf, Sep 18, 2006.

  1. asdf

    asdf Guest

    how to use c_str() ?

    if I compile the following program, there are errors.

    #include<iostream>
    #include<fstream>

    void main()
    {
    ofstream outfiloe(ofile.c_str());
    }
     
    asdf, Sep 18, 2006
    #1
    1. Advertising

  2. asdf

    Moonlit Guest

    add

    using namespace std;

    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "asdf" <> wrote in message
    news:...
    > how to use c_str() ?
    >
    > if I compile the following program, there are errors.
    >
    > #include<iostream>
    > #include<fstream>
    >
    > void main()
    > {
    > ofstream outfiloe(ofile.c_str());
    > }
    >
     
    Moonlit, Sep 18, 2006
    #2
    1. Advertising

  3. asdf wrote:

    > how to use c_str() ?
    >
    > if I compile the following program, there are errors.
    >
    > #include<iostream>
    > #include<fstream>
    >
    > void main()


    main returns int. Always.

    > {
    > ofstream outfiloe(ofile.c_str());


    What is ofile? Where did you define it? Without the definition, this
    is an error.

    ofstream resides in the std namespace, so you need to qualify it with
    "std::".

    Best regards,

    Tom


    > }
     
    Thomas Tutone, Sep 18, 2006
    #3

  4. > "asdf" <> wrote:


    > > how to use c_str() ?
    > >
    > > if I compile the following program, there are errors.
    > >
    > > #include<iostream>
    > > #include<fstream>
    > >
    > > void main()
    > > {
    > > ofstream outfiloe(ofile.c_str());
    > > }


    > >Moonlit wrote:

    > add
    >
    > using namespace std;


    Do you believe that adding that will fix the program?

    Best regards,

    Tom
     
    Thomas Tutone, Sep 18, 2006
    #4
  5. asdf

    Howard Guest

    "Moonlit" <news moonlit xs4all nl> wrote in message
    news:450ed792$0$4514$4all.nl...

    Please don't top-post. Place responses after (or interspersed with) what
    you're responding to. [re-arragned]


    >> how to use c_str() ?


    It's a member of std::string. Look it up.

    >>
    >> if I compile the following program, there are errors.
    >>
    >> #include<iostream>
    >> #include<fstream>
    >>
    >> void main()


    int main()

    >> {
    >> ofstream outfiloe(ofile.c_str());


    What's "ofile"?

    >> }


    > add
    >
    > using namespace std;
    >


    Ron, you didn't say where to add that. But better, in such a simple case,
    would be to add the std:: specifiers where needed, or to add using
    statements for the specific std:: items that will be used.

    But, there's more here wrong than just that.

    What's "ofile"? It's not defined. I'm guessing it's supposed to be a
    std::string, given the use of c_str(), but I don't see it declared as such
    anywhere. And the <string> header needs to be included if using strings.
    Also, the string needs to get its content filled somehow, such as by using
    std::cin.

    If this was an attempt at reducing what you're posting, you cut out too
    much. Read the FAQ, at http://www.parashift.com/c -faq-lite/ (especially
    section 5, reagarding how to post here for best results).

    And if you're getting errors you don't understand, post the error message
    (the text; error numbers mean nothing for people using other compilers), and
    tell us which line of code generates the error.

    Lastly, most of us probably don't have the book referred to in your subject
    line. Make your subject lines more specific to the problem (such as "error
    using c_str()" or "how do I use c_str()?"). In the body of your message,
    you can refer to the book you're getting an example from (but don't expect
    we'll all have that book).

    -Howard
     
    Howard, Sep 18, 2006
    #5
  6. asdf

    Default User Guest

    Moonlit wrote:

    > add


    Please don't top-post. Your replies belong following or interspersed
    with properly trimmed quotes. See the majority of other posts in the
    newsgroup, or the group FAQ list:
    <http://www.parashift.com/c++-faq-lite/how-to-post.html>
     
    Default User, Sep 18, 2006
    #6
  7. asdf

    Default User Guest

    asdf wrote:

    > how to use c_str() ?
    >
    > if I compile the following program, there are errors.
    >
    > #include<iostream>
    > #include<fstream>
    >
    > void main()
    > {
    > ofstream outfiloe(ofile.c_str());
    > }


    In the future, post complete programs. Do that by copying the actual
    code, not retyping. Also include the exact error messages.




    Brian
     
    Default User, Sep 18, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Koenig

    Announcement: C++ Primer, 4th edition

    Andrew Koenig, Jan 17, 2005, in forum: C++
    Replies:
    9
    Views:
    450
    red floyd
    Jan 19, 2005
  2. hajime

    C++ Primer, 4th Edition

    hajime, Aug 16, 2005, in forum: C++
    Replies:
    5
    Views:
    489
    hajime
    Aug 16, 2005
  3. asdf
    Replies:
    3
    Views:
    261
    KiLVaiDeN
    Oct 11, 2006
  4. arnuld
    Replies:
    17
    Views:
    621
    Marcus Kwok
    Oct 17, 2006
  5. asdf
    Replies:
    1
    Views:
    350
    Victor Bazarov
    Nov 21, 2006
Loading...

Share This Page