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

Emmanuel Delahaye

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.

There is no commands in C. Only statements and functions.
Is it only the two lines with comment need to be re-written??

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

This is, somehow, C. Make it good C, and there is no reason that it will not
be compilable on your platform.

For a start, add #include <stdio.h> at the top of the C file, and see what
happen.
 
C

Christopher Benson-Manica

news.hku.hk <[email protected]> spoke thus:

This is a C++ issue. I've directed posts in the direction of that
newsgroup, as comp.lang.c isn't interested.
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

You probably didn't include the correct C++ header. printf is declared
in <cstdio> on conforming C++ implementations. printf is legal C++,
so there's no inherent need to rewrite your code. However, I suggest
that you consider doing so anyway.
void _display_number(int v, int n){
^
You should not begin function names with an underscore; names
beginning with underscores are reserved for the implementation.
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) << std::setfill('0') << r;
}else{
printf("%s%d\n",n ? "-":"",v); // how to translate??

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

The trinary operator may or may not be advisable here; think about it.
 
M

Martin Ambuhl

news.hku.hk said:
can any one translate the following codes into c++ codes,

This is comp.lang.c. We do C, not C++. We do not translate C into
obfuscated languages. The good news is that your code already _is_ C++.
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??

Neither needs rewriting. You need to #include the appropriate headers.
And you would _still_ have to include appropriate headers if you
changed those lines.
 
J

J. J. Farrell

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.

Why not try to fix your trivial bug instead of translating the
program to a different language? Do you use a sledgehammer to
crack nuts? Putting

#include <stdio.h>

at the top should get rid of the 'implicit declaration' problem.
I'm not sure how running the printf command is relevant, but
there should be a man page for it on your system.
Is it only the two lines with comment need to be re-written??

Since you're asking about how to write C++, you'd be better
asking in a place that discusses C++.
 
D

Darrell Grainger

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.

I read this as, "Hi, I'm doing something that generates an error from the
C compiler. Rather than understand what this error is all about I'm just
going to try something different like code it in C++."

An implicit declaration usually implies you forgot to put:

#include <stdio.h>

at the top of your source code.
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<<...?

Wouldn't it make more sense to ask this question in the C++ newsgroup
where people are expected to program using std::cout? You could just ask
them how to print the number in the format you require. You don't even
need to mention the printf statement (unless you have no idea what it
does).
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top