Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   can't add to value ! .. urgent plz help (http://www.go4expert.com/forums/add-value-urgent-plz-help-t5382/)

coool 22Jul2007 06:03

can't add to value ! .. urgent plz help
 
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..

DaWei 22Jul2007 06:41

Re: can't add to value ! .. urgent plz help
 
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).

coool 22Jul2007 07:10

Re: can't add to value ! .. urgent plz help
 
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;
}


coool 22Jul2007 07:12

Re: can't add to value ! .. urgent plz help
 
oh I forgot to write [/code] in my previous post ! ..

I could not edit my previous post too ! there's no icon for that !

coool 22Jul2007 07:14

Re: can't add to value ! .. urgent plz help
 
you can try the program..

the output of 10010011 base 2 is 147 ;)
the output of BACD base 16 is 47821 :)

shabbir 22Jul2007 10:20

Re: can't add to value ! .. urgent plz help
 
Quote:

Originally Posted by coool
I could not edit my previous post too ! there's no icon for that !

You need 25 posts for that option to get enabled.


All times are GMT +5.5. The time now is 23:59.