First, INDENT your code.
#include<stdio.h>
main()
{
int n = 0, i = 0, x = 0, ax = 0, bx = 0, cx = 0, dx = 0, ex = 0, fx =
0, gx = 0, g[10], a[10], b[10], c[10], d[10], e[10], f[10];
char r[10];
printf("please enter the roman numeral :");
scanf("%s", &r);
for (; i < 10; i++) {
switch (r[i]) {
case 'I':
a[ax] = i;
++ax;
break;
case 'V':
b[bx] = i;
++bx;
break;
case 'X':
c[cx] = i;
++cx;
break;
case 'L':
d[dx] = i;
++dx;
break;
case 'C':
e[ex] = i;
++ex;
break;
case 'D':
f[fx] = i;
++fx;
break;
case 'M':
g[gx] = i;
++gx;
break;
default:
;
}
}
for (i = 0; r[i] != 0; i++) {
x++;
}
n = 1000 * (gx);
for (i = 0; i < fx; i++) {
if (gx == 0)
n = 500 * fx;
else if (f[i] > g[gx - 1])
n = n + 500;
else
n = n - 500;
}
for (i = 0; i < ex; i++) {
if (gx == 0 & fx == 0)
n = 100 * ex;
else if (fx == 0 & gx != 0 & e[i] < g[gx - 1])
n = n - 100;
else if (fx == 0 & gx != 0 & e[i] > g[gx - 1])
n = n + 100;
else if (fx = !0 & gx == 0 & e[i] < f[fx - 1])
n = n - 100;
else if (fx = !0 & gx == 0 & e[i] > f[fx - 1])
n = n + 100;
else if (fx = !0 & gx != 0 & e[i] > f[fx - 1] & e[i] > g[gx - 1])
n = n + 100;
else if (fx = !0 & gx != 0 & e[i] > g[gx - 1] & e[i] < f[fx - 1])
n = n - 100;
}
n = ((50 * dx) + n);
n = ((10 * cx) + n);
for (i = 0; i < cx; i++) {
if (c[i] < e[ex - 1] & c[i] < d[dx - 1])
n = n - 20;
}
n = ((5 * bx) + n);
n = ((1 * ax) + n);
for (i = 0; i < ax; i++) {
if (a[i] < b[bx - 1] & a[i] < c[cx - 1])
n = n - 2;
}
printf("value is %d ", n);
return 0;
} Second, don't use non-standard (and obsolete) headers like conio.h.
Third, use a decent compiler. I rather suspect this was tried on 20+ year old turbo c.
Modern compilers spot a lot more things, like
$ gcc -W -Wall -ansi -pedantic -O2 foo.c
foo.c:3: warning: return type defaults to ‘int’
foo.c: In function ‘main’:
foo.c:8: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[10]’
foo.c:56: warning: suggest brackets around comparison in operand of &
foo.c:58: warning: suggest brackets around comparison in operand of &
foo.c:58: warning: suggest brackets around comparison in operand of &
foo.c:60: warning: suggest brackets around comparison in operand of &
foo.c:60: warning: suggest brackets around comparison in operand of &
foo.c:62: warning: suggest brackets around comparison in operand of &
foo.c:62: warning: suggest brackets around comparison in operand of &
foo.c:62: warning: suggest brackets around assignment used as truth value
foo.c:64: warning: suggest brackets around comparison in operand of &
foo.c:64: warning: suggest brackets around comparison in operand of &
foo.c:64: warning: suggest brackets around assignment used as truth value
foo.c:66: warning: suggest brackets around comparison in operand of &
foo.c:66: warning: suggest brackets around comparison in operand of &
foo.c:66: warning: suggest brackets around comparison in operand of &
foo.c:66: warning: suggest brackets around assignment used as truth value
foo.c:68: warning: suggest brackets around comparison in operand of &
foo.c:68: warning: suggest brackets around comparison in operand of &
foo.c:68: warning: suggest brackets around comparison in operand of &
foo.c:68: warning: suggest brackets around assignment used as truth value
foo.c:74: warning: suggest brackets around comparison in operand of &
foo.c:80: warning: suggest brackets around comparison in operand of &
foo.c:8: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
foo.c:64: warning: array subscript is below array bounds
foo.c:66: warning: array subscript is below array bounds
foo.c:68: warning: array subscript is below array bounds The array bound warnings are extremely troublesome.
Possibly ALL the places you used &, you should have used &&
Fourth, it doesn't work with the prefix notation.
$ ./a.out
please enter the roman numeral :II
value is 2 $
$ ./a.out
please enter the roman numeral :XIX
value is 21 $ The second one should print 19, not 21.
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953