Hi I can't add to the same variable !! Code: unsigned long int Convert(char input[], int base) { unsigned long int value; int i; int n; n = 0; value = 0; for(i=strlen(input)-1;i>=0;i--) { switch(input[i]) { case 0: value =+ pow(base,n)*0; break; case 1: value =+ pow(base,n)*1; break; } n++; } return value; } the result of this binary input --> 101 is case 1: value=1 i= 2 n= 0 case 0: value=0 i= 0 n= 2 case 1: value=4 i= 0 n= 1 final answer = 4 ------ where final answer should be 5 it's not adding to value... it's just displaying the final digit calculation plz help.. I need to fix that in less than an hour..
The values of a text 0 ('0') and text 1 ('1') are not the same as the values 0 and 1. As a matter of fact, the value 0 ('\0' or nul) is the sign of the end of a string. You also have syntax errors. It's "+=", not "=+". You have so many errors that it's a wonder you got the program to run. Are you sure that you reproduced it correctly in the post? Make sure you have all errors and warnings enabled, and make a trip back to your books. Also, make sure that the term, 'base', is not confusing you. For "pow", it's not the numeric base, it's the number to be raised to the exponent (the second argument).
oh that's really strange ! my program run with this syntax error (=+) ! I've fixed it .. here's my program working fine.. - NOW i'm going to write the print function by the way do you have a way to do the convert function without having these many cases !! .. Code: #include <stdio.h> #include <math.h> #include <string.h> unsigned long int Convert(char[], int); int main() { char input[65]; unsigned long int value; int base; int val; do { printf("Enter a positive number: "); scanf("%64s",&input); printf("Enter the base: "); scanf("%d",&base); val = Check(input,base); if(val==1) printf("Thanks\t:)\nThe number you've entered matches the base you've selected!\n"); else printf("Error\t:(\nThe number you've entered doesn't match the base you've selected!\nPlease, try again..\n"); }while(val==0); value = Convert(input,base); //Print(value); //printf("%lu\n", value); return; } //void Print(unsigned long int value) //{ //} unsigned long int Convert(char input[], int base) { unsigned long int value; int i; int n; n = 0; value = 0; for(i=strlen(input)-1;i>=0;i--) { switch(input[i]) { case '0': value += pow(base,n)*0; break; case '1': value += pow(base,n)*1; break; case '2': value += pow(base,n)*2; break; case '3': value += pow(base,n)*3; break; case '4': value += pow(base,n)*4; break; case '5': value += pow(base,n)*5; break; case '6': value += pow(base,n)*6; break; case '7': value += pow(base,n)*7; break; case '8': value += pow(base,n)*8; break; case '9': value += pow(base,n)*9; break; case 'A': value += pow(base,n)*10; break; case 'B': value += pow(base,n)*11; break; case 'C': value += pow(base,n)*12; break; case 'D': value += pow(base,n)*13; break; case 'E': value += pow(base,n)*14; break; case 'F': value += pow(base,n)*15; break; } n++; } return value; } int Check(char input[], int base) { int i; switch(base) { case 2: for(i=strlen(input)-1;i>=0;i--) { if(!(input[i]=='1' || input[i]=='0')) return 0; } break; case 8: for(i=strlen(input)-1;i>=0;i--) { if(input[i]<'0' || input[i]>'7') return 0; } break; case 8: for(i=strlen(input)-1;i>=0;i--) { if(input[i]<'0' || input[i]>'7') return 0; } break; case 10: for(i=strlen(input)-1;i>=0;i--) { if(input[i]<'0' || input[i]>'9') return 0; } break; case 16: for(i=strlen(input)-1;i>=0;i--) { if(input[i]<'0' || (input[i]>'9' && input[i]<'A') || input[i]>'F') return 0; } break; } return 1; }
oh I forgot to write [/code] in my previous post ! .. I could not edit my previous post too ! there's no icon for that !