simple conversion from C to C++

N

news.hku.hk

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);
}
 
M

Mike Wahler

news.hku.hk said:
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
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
 
C

Christopher Benson-Manica

news.hku.hk said:
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.
 
N

news.hku.hk

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 !
 
J

John Harrison

news.hku.hk said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top