convert from c to c++

This is a discussion on convert from c to c++ within the C++ Programming forums, part of the General Programming Boards category; How can I convert this program in c to c++ code? /* itoa example */ #include <stdio.h> #include <stdlib.h> main ...

  1. #1
    Unregistered
    Guest

    convert from c to c++

    How can I convert this program in c to c++ code?



    /* itoa example */
    #include <stdio.h>
    #include <stdlib.h>

    main ()
    {
    int i;
    char buffer [33];
    printf ("Enter a number: ");
    scanf ("%d",&i);
    itoa (i,buffer,10);
    printf ("decimal: %s\n",buffer);
    itoa (i,buffer,16);
    printf ("hexadecimal: %s\n",buffer);
    itoa (i,buffer,2);
    printf ("binary: %s\n",buffer);
    return 0;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    #include <stdio.h>
    #include <stdlib.h>

    int main ()
    {
    int i;
    char buffer [33];
    printf ("Enter a number: ");
    scanf ("%d",&i);
    itoa (i,buffer,10);
    printf ("decimal: %s\n",buffer);
    itoa (i,buffer,16);
    printf ("hexadecimal: %s\n",buffer);
    itoa (i,buffer,2);
    printf ("binary: %s\n",buffer);
    return 0;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    8
    #include <iostream>
    #include <cstdlib>

    int main() {

    int i;
    char buffer[33];

    cout >> "Please enter a number: ";
    cin << i;

    itoa( i , buffer, 10);

    cout << "Decimal: " << i;

    itoa(1, buffer, 16);

    cout << "Hexadecimal: " << i;

    itoa(i, buffer, 2);

    cout << "Binary: " << i << endl;

    return 0;

    }

    I'm pretty sure this will work.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Actually, I think it would be this

    #include <cstdio>
    #include <cstdlib>

    int main ()
    {
    int i;
    char buffer [33];
    printf ("Enter a number: ");
    scanf ("%d",&i);
    itoa (i,buffer,10);
    printf ("decimal: %s\n",buffer);
    itoa (i,buffer,16);
    printf ("hexadecimal: %s\n",buffer);
    itoa (i,buffer,2);
    printf ("binary: %s\n",buffer);
    return 0;
    }



    Honestly though, I actually have to check look up all the printf identifiers before I can read that code.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Come on. You C++er's can do better than that. Even I know that you can use:

    string buffer;

    And:

    buffer + i;

    But I have no idea how to replace the itoa functions. There must be methods that will do that.

  6. #6
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101

    Re: convert from c to c++

    is this what you mean?
    Code:
    #include <iostream.h>
    
    int main ()
    {
      int i;
      char buffer [33];
      cout<<"Enter a number: ";
      cin>>i;
      itoa (i,buffer,10);
      cout<<"decimal: "<<buffer<<"\n";
      itoa (i,buffer,16);
      cout<<"hexadecimal: "<<buffer<<"\n";
      itoa (i,buffer,2);
      cout<<"binary: "<<buffer<<"\n";
      return 0;
    }
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

  7. #7
    Unregistered
    Guest

    i recieved an error

    : error C2065: 'itoa' : undeclared identifier

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    add:
    #include<stdlib.h>

  9. #9
    Unregistered
    Guest

    Thanks, another question

    Thanks, another question:

    cout<<"octaldecimal: "<<buffer<<"\n";
    itoa (i,buffer,8);


    Is this the right code for octal?

  10. #10
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You C++er's can do better than that.
    I think their point is that the code is c++ already. But if you wanted to use some exclusive c++ stuff you could do this -

    Code:
    #include <iostream>
    #include <bitset>
    
    using namespace std;
    
    int main()
    {
    
    	int i;
        cout<<"Enter a number: ";
    	cin>>i;
    	bitset<32> bin(i);
    
    	cout << "decimal: " << i << endl;
    	cout << "hexadecimal: " << hex << i <<endl;
    	cout << "binary: " << bin << endl;
    
    
    	return 0;
    }
    zen

  11. #11
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    For octal you can do -

    cout << "octal: " << oct << i << endl;
    zen

  12. #12
    Unregistered
    Guest
    Thanks a whole lot, it works.

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Well I'll be damned. That's fantastic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Convert char* time to const tm*
    By stickman in forum C++ Programming
    Replies: 10
    Last Post: 11-14-2006, 09:24 PM
  3. Replies: 3
    Last Post: 08-21-2006, 07:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 11:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21