Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > simple conversion from C to C++

Reply
Thread Tools

simple conversion from C to C++

 
 
news.hku.hk
Guest
Posts: n/a
 
      04-30-2004
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);
}


 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      04-30-2004

"news.hku.hk" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-30-2004
Mike Wahler <(E-Mail Removed)> 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.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-30-2004
news.hku.hk <(E-Mail Removed)> 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.
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      04-30-2004

"Christopher Benson-Manica" <(E-Mail Removed)> wrote in message
news:c6u6al$3r8$(E-Mail Removed)...
> Mike Wahler <(E-Mail Removed)> 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


 
Reply With Quote
 
news.hku.hk
Guest
Posts: n/a
 
      05-01-2004
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:ut);
if (!ofs){
cerr << "Error" << endl;
return 1;
}
display_number(1234567; // 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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);
> }
>
>



 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-01-2004

"news.hku.hk" <(E-Mail Removed)> wrote in message
news:40931a87$(E-Mail Removed)...
> 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:ut);
> if (!ofs){
> cerr << "Error" << endl;
> return 1;
> }
> display_number(1234567; // 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:ut);
if (!ofs){
cerr << "Error" << endl;
return 1;
}
display_number(ofs, 1234567;

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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion xhtml champs Python 0 06-21-2011 11:59 AM
PSD to XHTML Conversion Services and PSD to HTML CSS ConversionServices, PSD to Joomla, Drupal, Wordpress Conversion PSD to XHTML Conversion Services and PSD to HTML CSS Conversion Services, PSD to Joomla, Drupal, Wor VHDL 0 04-25-2011 06:43 AM
conversion operator and conversion ctor subramanian100in@yahoo.com, India C++ 2 09-15-2009 12:46 PM
Simple String Conversion? Gary ASP .Net 2 02-15-2006 12:32 PM
Simple problem with loop and asp conversion =?Utf-8?B?VGltOjouLg==?= ASP .Net 0 05-27-2004 08:36 AM



Advertisments