Convert this C program to C++

How to convert this program properly?

Page 1 of 1

0 Replies - 661 Views - Last Post: 24 October 2008 - 11:10 AM Rate Topic: -----

#1 gedinfo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-October 08

Convert this C program to C++

Post icon  Posted 24 October 2008 - 11:10 AM

I am attempting to convert this program from C code(which was running under DOS without any issues) into C++.
The second shift of _rotr() causes _itoa_s() to fail.
Any suggestions?

Thank you

/* ROTATE.C illustrates bit rotation functions including:
 *	  _rotl		   _rotr		   _lrotl		  _lrotr
 *
 * _lrotl and _lrotr are not shown in the program, but they are the
 * same as _rotl and _rotr except that they work on long integers.
 */

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

char *binstr( unsigned int num, char *buffer );	  /* Prototype */
void display_screen(void);
char ch;
int main(void)
{
	display_screen();
	while (ch != 'n')
	{
		ch = _getch();
		if (ch != 'n')
			display_screen();
	}
	return 0;
}
void display_screen()
{
	unsigned int i, ir, il;
	int  shift, result;
	char tmpbuf[50];

	printf( "Enter integer: " );
	
	result = scanf_s( "%i", &i,16 );
	if (result)
	{
	/* Display table header for rotates. */
	printf( "%6s	%-7s	%16s	%-7s	%16s\n"," ", "Left", " ", "Right", " " );
	printf( "%6s	%7s	%16s	%7s	%16s\n\n","Shift", "Decimal", "Binary", "Decimal", "Binary" );
	   /* Display table of rotated values. */
	for( shift = 0; shift <= 16; shift++ )
	   {
	   il = _rotl( i, shift ); 
	   printf( "%5d	%7d	%16s	", shift, il, binstr( il,  tmpbuf ) );
	   ir = _rotr( i, shift );
	   printf( "%7d	%16s\n", ir, binstr( ir, tmpbuf ) );
	   }
	printf("Press any key except 'n' to continue. \n");
	printf( "\n\n" );
	}
	else
	{
	   printf("ENTER ONLY INTEGERS\n");
	   printf("typing n will allow you to exit\n");
	}
}
/* Converts integer to string of 16 binary characters. */
char *binstr( unsigned int num, char *buffer )
{
	char tmp[17];
	errno_t IVal;
	size_t len;

	memset( buffer, '0', 16 );
	IVal = _itoa_s( num, tmp, 17, 2 );
	len = strlen((const char *)tmp);
	strcpy_s( ((buffer + 16) - len), 16, tmp );
	return buffer;
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1