A different approach...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int relay = 0;
int i;
for ( i = 0; argv[1][i]; ++i )
{
int x = 1 << (argv[1][i] - '1 ');
printf("x = %d\n", x);
relay += x;
}
printf("relay = %d\n", relay);
return 0;
}
/* my output
C:\Test>test 46
x = 8
x = 32
relay = 40
C:\Test>test 137
x = 1
x = 4
x = 64
relay = 69
*/
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
Thanks, but I can't get your code to work, first i get this warning in gcc:
test.c:11:34: warning: multi-character character constant
int x = 1 << (argv[1][i] - '1 ');
Hmm. How'd that extra space get in there? It should be like this.
int x = 1 << (argv[1][i] - '1');
I don't know if that solves the issue. But do you see generally what the code was trying to do?
That is, take each character of the number entered. Subtracting'1' will from '1' - '8' will give a value from 0 - 7. Left shifting a 1 by this amount should calculate a value like your table. The loop just sums 'em up.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314