473,382 Members | 1,247 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,382 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 1580

"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
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.