Thread: C to C++ translation help?

  1. #1
    Registered User
    Join Date
    Oct 2007

    C to C++ translation help?

    i found this program and i'm more familiar with c++ but this is in c and i just can't figure out how to put it into c++

    /* Demonstration of the do..while loop */
        #include <stdio.h>
        int main(void)
            int value, r_digit;
            printf(Enter a number to be reversed.\n);
            scanf(%d, &value);
                    r_digit = value % 10;
                    printf(%d, r_digit);
                    value = value / 10;
                } while (value != 0);
            return 0;

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Use cout instead of printf(), cin instead of scanf(), and include iostream instead of stdio.h. Lastly, to be completely portable, I believe main() should accept nothing, not even void. For C, void is preferred.

    So yeah... That's easy.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Quote Originally Posted by MacGyver View Post
    Lastly, to be completely portable, I believe main() should accept nothing, not even void.
    Your belief is incorrect. In C++ "int main()" is equivalent to "int main(void)".

    There is also the minor point that no return statement is required from main() in C++: falling off the end is equivalent to returning zero. That is also true in the 1999 C standard but not, IIRC, in the 1989 C standard.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Although using void for a function argument is allowed in C++, I believe it's generally considered bad style, since the only reason for it is to avoid the ambiguity caused by C's backwards compatibility with the old K&R function declarations, and those aren't allowed in C++. So to make the code fully C++-like, the void should probably be taken out.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Last I checked, "bad style C++" (something some mere mortals will frown upon) is a subjective notion that differs from "invalid C++" (something that, arguably, can be defined objectively by reference to the C++ standard).

    I have seen some C++ style guides (admittedly ones I don't subscribe to) that require functions with no arguments to be declared with a void argument list. One of the reasons, stated in one of those style guides, is that a fair amount of C++ code interfaces to legacy C code.

  6. #6
    Amazingly beautiful user.
    Join Date
    Jul 2005
    If you knew I'd have to kill you
    If you want to ignore style (a hugely important part of programming), than why not just leave it with printf and scanf? I mean, <cstdio> has them, so, it's perfectly valid C++. Of course, if you actually want to program in C++, taking out the "void" would be a good idea, along with switching to cin, cout, etc.
    Programming Your Mom.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  2. How to find relative translation...
    By Shamino in forum Game Programming
    Replies: 5
    Last Post: 05-20-2007, 09:39 PM
  3. Website translation
    By Jumper in forum A Brief History of
    Replies: 5
    Last Post: 03-17-2004, 09:34 PM
  4. msvc translation unit
    By Stoned_Coder in forum A Brief History of
    Replies: 3
    Last Post: 05-14-2003, 05:31 AM
  5. Translation
    By GaPe in forum C Programming
    Replies: 10
    Last Post: 04-04-2002, 09:45 AM