0

The first code I am posting is supposed to write 222 which is workign fne,I have similar code as part of second program that I am posting which is not working in fact giving me negative result,running on cygwin,trying to get ascii # of a str

int array[]={2,2,2};
int ugh=0;
int ok=2;
for(int i=0;i<3;i++)
    {
        ugh+= (array[i]*pow(10,(ok-i)));
    }


int array[size];
    int mesnum=0;
for (int i=0;i<size;i++)
    {
        array[i]=int(message[i]);
        cout<<array[i]<<endl;
    }
     int something=size-1;
     cout<<something;
    for(int i=0;i<size;i++)
    {
        mesnum+= (array[i]*pow(10,(something-i)));
        cout<<mesnum<<endl;
    }

    return mesnum;
  • btw conversion from string to int array was fine, and 1st 7 line is of 1st program and then long break then 2nd program –  Mar 29 '17 at 04:06
  • If your message[i] is a char then array[i] will store its ascii value.You need to do it like array[i]=message[i]-'0'; – user1438832 Mar 29 '17 at 04:09
  • [Do not use `pow` when the exponents are integer](https://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os) – PaulMcKenzie Mar 29 '17 at 04:10
  • @user1438832 thanks that's a smart way did not come in my mind first –  Mar 30 '17 at 12:13
  • @PaulMcKenzie but may I know why you suggest that, it actually worked –  Mar 30 '17 at 12:14
  • @Nayema Read the link in my comment. There is no *guarantee* it will work correctly. The key word is *guarantee*. In addition, `pow` works on floating point values, giving a chance of inaccurate results (again, read the link). `pow` also runs more slowly than a simple lookup table or just multiplied the number by itself x times. – PaulMcKenzie Mar 30 '17 at 14:54
  • hi thanks so much for giving me this info :) –  Apr 03 '17 at 01:36

0 Answers0