simple conversion from C to C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • news.hku.hk

    #1

    simple conversion from C to C++

    can any one translate the following codes into c++ codes, coz i can't run
    the "printf" command in Unix and it said it's implicit declaration.
    Is it only the two lines with comment need to be re-written??
    Thanks a lot

    void _display_number (int v, int n){
    if(v >= 1000){
    int r = v % 1000;
    _display_number (v / 1000,n);
    printf(",%03d", r); // how to translate that into
    std::cout<<...?
    }else{
    printf("%s%d\n" ,n ? "-":"",v); // how to translate??
    }
    }

    void display_number( int v){
    _display_number (v < 0 ? -v : v,v < 0);
    }



  • Emmanuel Delahaye

    #2
    Re: simple conversion from C to C++

    In 'comp.lang.c', "news.hku.h k" <billychu@hkusu a.hku.hk> wrote:
    [color=blue]
    > can any one translate the following codes into c++ codes, coz i can't
    > run the "printf" command in Unix and it said it's implicit declaration.[/color]

    There is no commands in C. Only statements and functions.
    [color=blue]
    > Is it only the two lines with comment need to be re-written??
    >
    > void _display_number (int v, int n){
    > if(v >= 1000){
    > int r = v % 1000;
    > _display_number (v / 1000,n);
    > printf(",%03d", r); // how to translate that[/color]

    This is, somehow, C. Make it good C, and there is no reason that it will not
    be compilable on your platform.

    For a start, add #include <stdio.h> at the top of the C file, and see what
    happen.

    --
    -ed- get my email here: http://marreduspam.com/ad672570
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=cpp
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

    Comment

    • Christopher Benson-Manica

      #3
      Re: simple conversion from C to C++

      news.hku.hk <billychu@hkusu a.hku.hk> spoke thus:

      This is a C++ issue. I've directed posts in the direction of that
      newsgroup, as comp.lang.c isn't interested.
      [color=blue]
      > can any one translate the following codes into c++ codes, coz i can't run
      > the "printf" command in Unix and it said it's implicit declaration.
      > Is it only the two lines with comment need to be re-written??
      > Thanks a lot[/color]

      You probably didn't include the correct C++ header. printf is declared
      in <cstdio> on conforming C++ implementations . printf is legal C++,
      so there's no inherent need to rewrite your code. However, I suggest
      that you consider doing so anyway.
      [color=blue]
      > void _display_number (int v, int n){[/color]
      ^
      You should not begin function names with an underscore; names
      beginning with underscores are reserved for the implementation.
      [color=blue]
      > if(v >= 1000){
      > int r = v % 1000;
      > _display_number (v / 1000,n);
      > printf(",%03d", r); // how to translate that into
      > std::cout<<...?[/color]

      std::cout << "," << std::setw(3) << std::setfill('0 ') << r;
      [color=blue]
      > }else{
      > printf("%s%d\n" ,n ? "-":"",v); // how to translate??[/color]

      std::cout << n?"-":"" << v;

      The trinary operator may or may not be advisable here; think about it.
      [color=blue]
      > }
      > }[/color]
      [color=blue]
      > void display_number( int v){
      > _display_number (v < 0 ? -v : v,v < 0);
      > }[/color]




      --
      Christopher Benson-Manica | I *should* know what I'm talking about - if I
      ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

      Comment

      • Martin Ambuhl

        #4
        Re: simple conversion from C to C++

        news.hku.hk wrote:
        [color=blue]
        > can any one translate the following codes into c++ codes,[/color]

        This is comp.lang.c. We do C, not C++. We do not translate C into
        obfuscated languages. The good news is that your code already _is_ C++.
        [color=blue]
        > coz i can't run
        > the "printf" command in Unix and it said it's implicit declaration.[/color]

        That's because you forgot to #include <stdio.h> (in C) or <cstdio> (in
        C++, shudder)
        [color=blue]
        > Is it only the two lines with comment need to be re-written??[/color]

        Neither needs rewriting. You need to #include the appropriate headers.
        And you would _still_ have to include appropriate headers if you
        changed those lines.
        [color=blue]
        > Thanks a lot
        >
        > void _display_number (int v, int n){
        > if(v >= 1000){
        > int r = v % 1000;
        > _display_number (v / 1000,n);
        > printf(",%03d", r); // how to translate that into
        > std::cout<<...?
        > }else{
        > printf("%s%d\n" ,n ? "-":"",v); // how to translate??
        > }
        > }
        >
        > void display_number( int v){
        > _display_number (v < 0 ? -v : v,v < 0);
        > }
        >
        >
        >[/color]

        Comment

        • J. J. Farrell

          #5
          Re: simple conversion from C to C++

          "news.hku.h k" <billychu@hkusu a.hku.hk> wrote in message news:<409282f6$ 2@newsgate.hku. hk>...[color=blue]
          > can any one translate the following codes into c++ codes, coz i can't run
          > the "printf" command in Unix and it said it's implicit declaration.[/color]

          Why not try to fix your trivial bug instead of translating the
          program to a different language? Do you use a sledgehammer to
          crack nuts? Putting

          #include <stdio.h>

          at the top should get rid of the 'implicit declaration' problem.
          I'm not sure how running the printf command is relevant, but
          there should be a man page for it on your system.
          [color=blue]
          > Is it only the two lines with comment need to be re-written??[/color]

          Since you're asking about how to write C++, you'd be better
          asking in a place that discusses C++.
          [color=blue]
          > Thanks a lot
          >
          > void _display_number (int v, int n){
          > if(v >= 1000){
          > int r = v % 1000;
          > _display_number (v / 1000,n);
          > printf(",%03d", r); // how to translate that into
          > std::cout<<...?
          > }else{
          > printf("%s%d\n" ,n ? "-":"",v); // how to translate??
          > }
          > }
          >
          > void display_number( int v){
          > _display_number (v < 0 ? -v : v,v < 0);
          > }[/color]

          Comment

          • Darrell Grainger

            #6
            Re: simple conversion from C to C++

            On Sat, 1 May 2004, news.hku.hk wrote:
            [color=blue]
            > can any one translate the following codes into c++ codes, coz i can't run
            > the "printf" command in Unix and it said it's implicit declaration.[/color]

            I read this as, "Hi, I'm doing something that generates an error from the
            C compiler. Rather than understand what this error is all about I'm just
            going to try something different like code it in C++."

            An implicit declaration usually implies you forgot to put:

            #include <stdio.h>

            at the top of your source code.
            [color=blue]
            > Is it only the two lines with comment need to be re-written??
            > Thanks a lot
            >
            > void _display_number (int v, int n){
            > if(v >= 1000){
            > int r = v % 1000;
            > _display_number (v / 1000,n);
            > printf(",%03d", r); // how to translate that into
            > std::cout<<...?[/color]

            Wouldn't it make more sense to ask this question in the C++ newsgroup
            where people are expected to program using std::cout? You could just ask
            them how to print the number in the format you require. You don't even
            need to mention the printf statement (unless you have no idea what it
            does).
            [color=blue]
            > }else{
            > printf("%s%d\n" ,n ? "-":"",v); // how to translate??
            > }
            > }
            >
            > void display_number( int v){
            > _display_number (v < 0 ? -v : v,v < 0);
            > }[/color]

            --
            Send e-mail to: darrell at cs dot toronto dot edu
            Don't send e-mail to vice.president@ whitehouse.gov

            Comment

            Working...