473,320 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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);
}
Jul 22 '05 #1
6 1579

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40******@newsgate.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.
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
Jul 22 '05 #2
Mike Wahler <mk******@mkwahler.net> 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.
Jul 22 '05 #3
news.hku.hk <bi******@hkusua.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.
Jul 22 '05 #4

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c6**********@chessie.cirr.com...
Mike Wahler <mk******@mkwahler.net> 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
Jul 22 '05 #5
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::out);
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" <bi******@hkusua.hku.hk> wrote in message
news:40******@newsgate.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);
}

Jul 22 '05 #6

"news.hku.hk" <bi******@hkusua.hku.hk> wrote in message
news:40********@newsgate.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::out);
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::out);
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
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: news.hku.hk | last post by:
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...
5
by: Jazper | last post by:
hi can anybody explain that to me: i did the following: - 1. input of char 244 ("ô") into string - 2. convert to getbytes - 3. convert back to string char 244 changed to char 63 ("?")...
5
by: Russ | last post by:
Hi guys & gals I need to convert, in C#, a decimal hex string into an ascii string (ie "56" -> "V"), and I can't find anything in the vs2003 help files that is remotely helpful. Any suggestions...
1
by: Rudy | last post by:
Hello all! I tried a few things, can't seem to figure out the right combination. here the snippet of CODE Dim cmdBet As SqlCommand Dim ConWg As New SqlConnection Try ConWg = New...
0
by: Steve | last post by:
Microsoft has introduced a Persian Calendar Class in ASP.net 2.0 http://msdn2.microsoft.com/en-us/library/system.globalization.persiancalendar.aspx I have looked at the methods and the only way...
1
by: Phillip Vong | last post by:
I have a simple Formview and in the InsertMode I have a calendar and a textbox. All I want is for people to click on a date and the date is populated to the textbox. Here is my simple code and my...
4
by: Phillip Vong | last post by:
VS2005 in VB.NET I have 2 simple textboxes (Textbox1, Textbox2) and they both have dates. Textbox1=12/31/2006 Textbox2=12/15/2006 I need to convert these two textboxes that are in String...
4
by: LoneHunter01 | last post by:
I need to be able to enter a string and make it go to all lowercase. The only way I think you can do that is by first converting into char's. I've seen alot of "string to char array" but no simple...
6
by: Lasse Reichstein Nielsen | last post by:
Max <adsl@tiscali.itwrites: Not really. It shows that a particularly naïve implementation of a conversion from XML to JSON doesn't work well. What if the conversion of <e> some
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.