simple conversion from C to C++

Discussion in 'C++' started by news.hku.hk, Apr 30, 2004.

  1. news.hku.hk

    news.hku.hk Guest

    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);
    }
     
    news.hku.hk, Apr 30, 2004
    #1
    1. Advertising

  2. news.hku.hk

    Mike Wahler Guest

    "news.hku.hk" <> wrote in message
    news:...
    > 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.


    Probably because you failed to #include the required
    header, <cstdio> or <stdio.h>. 'printf()' works just
    fine with C++.

    > 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<<...?


    std::cout << ',' << std::setw(3) << r;


    > }else{
    > printf("%s%d\n",n ? "-":"",v); // how to translate


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

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


    -Mike
     
    Mike Wahler, Apr 30, 2004
    #2
    1. Advertising

  3. Mike Wahler <> spoke thus:

    >> printf(",%03d",r); // how to translate that


    > std::cout << ',' << std::setw(3) << r;


    Note that the default fill character is a space - I think you forgot

    std::setfill('0');

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 30, 2004
    #3
  4. news.hku.hk <> spoke thus:

    > 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??


    Please don't multipost in the future. Thanks.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Apr 30, 2004
    #4
  5. news.hku.hk

    Mike Wahler Guest

    "Christopher Benson-Manica" <> wrote in message
    news:c6u6al$3r8$...
    > Mike Wahler <> spoke thus:
    >
    > >> printf(",%03d",r); // how to translate that

    >
    > > std::cout << ',' << std::setw(3) << r;

    >
    > Note that the default fill character is a space - I think you forgot
    >
    > std::setfill('0');


    Yes,thanks.

    -Mike
     
    Mike Wahler, Apr 30, 2004
    #5
  6. news.hku.hk

    news.hku.hk Guest

    Thanks much for all your suggestion. And i won't multipost anymore. Just
    another question. As printf is for output to screen, what if i want to
    output the result into a text file.

    i've written:
    __________________________________
    ofstream of ("abc.txt", ios::eek:ut);
    if (!ofs){
    cerr << "Error" << endl;
    return 1;
    }
    display_number(12345678); // this just output to screen. But how to change
    the function "display_number" to output the result to abc.txt???or is there
    any simpler ways ????
    ________________________________
    Thanks all !

    "news.hku.hk" <> wrote in message
    news:...
    > 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);
    > }
    >
    >
     
    news.hku.hk, May 1, 2004
    #6
  7. "news.hku.hk" <> wrote in message
    news:40931a87$...
    > Thanks much for all your suggestion. And i won't multipost anymore. Just
    > another question. As printf is for output to screen, what if i want to
    > output the result into a text file.
    >
    > i've written:
    > __________________________________
    > ofstream of ("abc.txt", ios::eek:ut);
    > if (!ofs){
    > cerr << "Error" << endl;
    > return 1;
    > }
    > display_number(12345678); // this just output to screen. But how to change
    > the function "display_number" to output the result to abc.txt???or is

    there
    > any simpler ways ????


    This is a simple way, this is the only way in C++. You need to pass the
    destination you want to display to as a parameter to display_number and
    _display_number, that means you are going to have to rewrite _display_number
    to use C++ I/O instead of printf.

    void _display_number(ostream& os, int v, int n){
    ...
    }


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

    ofstream ofs("abc.txt", ios::eek:ut);
    if (!ofs){
    cerr << "Error" << endl;
    return 1;
    }
    display_number(ofs, 12345678);

    Now I think you should have a go at rewriting _display_number yourself.
    Instead of cout << ..., you use the parameter passed in like this, os << ...

    john
     
    John Harrison, May 1, 2004
    #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. =?Utf-8?B?VGltOjouLg==?=

    Simple problem with loop and asp conversion

    =?Utf-8?B?VGltOjouLg==?=, May 27, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    381
    =?Utf-8?B?VGltOjouLg==?=
    May 27, 2004
  2. Gary

    Simple String Conversion?

    Gary, Feb 15, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    435
    Karl Seguin [MVP]
    Feb 15, 2006
  3. Newsgroup - Ann

    simple type conversion question

    Newsgroup - Ann, Aug 7, 2003, in forum: C++
    Replies:
    1
    Views:
    287
    Alf P. Steinbach
    Aug 7, 2003
  4. Mendon?a
    Replies:
    2
    Views:
    1,640
    klaas
    Oct 9, 2003
  5. , India
    Replies:
    2
    Views:
    478
    Fraser Ross
    Sep 15, 2009
Loading...

Share This Page