:cry: I have spent hours on this program and cannot get it to produce negatives or INT_MIN. Here is my code. Any help would be so appreciated I can't even begin to tell you...

#include <stdio.h>
#include <iostream>
using namespace std; 
char *strrev(char *);
char *itoa_3(int, char *, int);
int main()
{
char A[40]; 
cout << "cout base 10" << endl; 
cout << 17 << endl; 
cout << 0 << endl; 
cout << -17 << endl; 
cout << INT_MAX << endl; 
cout << INT_MIN << endl; 
cout << "\ncout base 16" << endl; 
cout << hex << 17 << endl; 
cout << 0 << endl; 
cout << -17 << endl; 
cout << INT_MAX << endl; 
cout << INT_MIN << endl; 
cout << dec; 
cout << "\nitoa_3 base 2" << endl; 
cout << itoa_3(17, A, 2) << endl; 
cout << itoa_3(0, A, 2) << endl; 
cout << itoa_3(-17, A, 2) << endl; 
cout << itoa_3(INT_MAX, A, 2) << endl; 
cout << itoa_3(INT_MIN, A, 2) << endl; 
cout << "\nitoa_3 base 10" << endl; 
cout << itoa_3(17, A, 10) << endl; 
cout << itoa_3(0, A, 10) << endl; 
cout << itoa_3(-17, A, 10) << endl; 
cout << itoa_3(INT_MAX, A, 10) << endl; 
cout << itoa_3(INT_MIN, A, 10) << endl; 
cout << "\nitoa_3 base 16" << endl; 
cout << itoa_3(17, A, 16) << endl; 
cout << itoa_3(0, A, 16) << endl; 
cout << itoa_3(-17, A, 16) << endl; 
cout << itoa_3(INT_MAX, A, 16) << endl; 
cout << itoa_3(INT_MIN, A, 16) << endl; 
cout << "\nitoa_3 base 32" << endl; 
cout << itoa_3(17, A, 32) << endl; 
cout << itoa_3(0, A, 32) << endl; 
cout << itoa_3(-17, A, 32) << endl; 
cout << itoa_3(INT_MAX, A, 32) << endl; 
cout << itoa_3(INT_MIN, A, 32) << endl; 
cout << "\nitoa_3 base 36" << endl; 
cout << itoa_3(17, A, 36) << endl; 
cout << itoa_3(0, A, 36) << endl; 
cout << itoa_3(-17, A, 36) << endl; 
cout << itoa_3(INT_MAX, A, 36) << endl; 
cout << itoa_3(INT_MIN, A, 36) << endl; 
return 0; 
} 
char *strrev(char *str) {
char *p1, *p2;
if (!str || !*str)
return str;
for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) {
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return str;
}
char *itoa_3(int n, char *s, int b) {
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
int i=0, sign;

if ((sign = n) < 0)
n = -n;
do {
s[i++] = digits[n % b];
} while ((n /= b) != 0);
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
return strrev(s);
}

What exactly is wrong? There's a few minor errors e.g. change <stdio.h> to <cstdio> (why do you need this and iostream?) and put a space between using and namespace, but it runs. Here's the output I get:

$ ./a.out 
cout base 10
17
0
-17
2147483647
-2147483648

cout base 16
11
0
ffffffef
7fffffff
80000000

itoa_3 base 2
10001
0
-10001
1111111111111111111111111111111
0000000000000000000000000000000

itoa_3 base 10
17
0
-17
2147483647


itoa_3 base 16
11
0
-11
7fffffff
0000000

itoa_3 base 32
h
0
-h
1vvvvvv
000000

itoa_3 base 36
h
0
-h
zik0zj

Are these results correct?

PS - nice try on the [code] tags. Edit it if you can, and maybe preview next time? ;)

Edited 3 Years Ago by Dani: Formatting fixed

This article has been dead for over six months. Start a new discussion instead.