Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: problem wit...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 4 Topic 45763 of 47034
Post > Topic >>

Re: problem with a reverse iterator

by "Victor Bazarov" <v.Abazarov@[EMAIL PROTECTED] > May 5, 2008 at 06:00 PM

brad wrote:
> In the example below, possible_number is a string of one or more
> digit(s). For example, it may be 4 or 4444. However, when I attempt to
> perform the times 2 calculation, the ASCII value of 4 (which is 52) is
> used instead of 4 itself. I need to be able to easily reverse the
> order of the digits and to only perform this calculation on even
> positioned digits... that's why I'm using string instead of int. Any
> suggestions on how to make 4*2 = 8 I've tried atoi() type casting,
> etc. I'm stuck. Still learning c++.
>
> Thanks for any help!
>
>   int position = 0;
>
>   string::reverse_iterator rit;
>   for (rit=possible_number.rbegin(); rit < possible_number.rend();
>       rit++) {

First off, try using more of ++rit, it's better than rit++.

>       if (position % 2 == 0)
>             {
>             // Here's the problem *rit * 2 = 104, not 8
>             cout << position << *rit * 2 << endl;

An element of a string is of type 'char'.  As you have found out
already, the value is controlled by the encoding of the symbol.
If you want to convert a char that supposedly contains a decimal
digit into its corresponding number, you need to (a) test to see
that it's actually a digit (see 'isdigit' function) and (b) if it
is a digit, subtract '0' from it:


             if (isdigit(*rit))
                 cout << position << (*rit - '0') * 2 << endl;
             else
                 cerr << position << " - not a digit!!!" << endl;

>             position++;
>             }
>       }

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 




 4 Posts in Topic:
problem with a reverse iterator
brad <byte8bits@[EMAIL  2008-05-05 17:55:44 
Re: problem with a reverse iterator
"Victor Bazarov"  2008-05-05 18:00:34 
Re: problem with a reverse iterator
brad <byte8bits@[EMAIL  2008-05-05 18:21:52 
Re: problem with a reverse iterator
Pete Becker <pete@[EMA  2008-05-05 20:34:50 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:01:22 CDT 2008.