How to get this to run under C++?

Joined
Oct 24, 2008
Messages
1
Reaction score
0
The code below used to work on a DOS system. I am attempting to compile it for C++, and I have changed some of the deprecated functions to xxx_s, but the strlen craps out at the second loop.

Any ideas on how to get this to run under Visual C++ 2008 Express Edition?

Thank you.

Code:
/* 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( "%i", &i );
    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];
    int  len;

    memset( buffer, '0', 16 );
    len = strlen( _itoa( num, tmp, 2 ) );
    strcpy( buffer + 16 - len, tmp );
    return buffer;
}
 

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

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top