470,849 Members | 1,253 Online

# Numerical value of a string

Hi all,

I am trying to find the numerical value of a string that stores a two
digit number. I have found the numerical value of a char as:

char character;
cin >character;
int number = character - 48; // Computing the numberical value of
character entered
cout << "The number you entered is: " << number << endl << endl;

How do I do the same and find the numerical value of a string storing 2
digits? I know a way to do this using character arrays:
for ( ; *str != '\0' ; str++)
num = (num * 10) + (*str - 48);

But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program.

I am stumped. Any ideas?

Thanks,
Farah.

Oct 13 '06 #1
8 2051 fa**********@gmail.com wrote:
I am trying to find the numerical value of a string that stores a two
digit number. I have found the numerical value of a char as:

char character;
cin >character;
int number = character - 48; // Computing the numberical value of
character entered
cout << "The number you entered is: " << number << endl << endl;

How do I do the same and find the numerical value of a string storing 2
digits? I know a way to do this using character arrays:
for ( ; *str != '\0' ; str++)
num = (num * 10) + (*str - 48);

But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program.

I am stumped. Any ideas?
See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-39.2

Cheers! --M

Oct 13 '06 #2
mlimber wrote:
fa**********@gmail.com wrote:
>I am trying to find the numerical value of a string that stores a two
digit number.

See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-39.2

Cheers! --M
Before c.l.c++ I had never seen a group whose FAQs were quite so FA, if you see
what I mean... This one seems to come up every 2 days, along with "I need to
write a dynamic array". I actually discovered the group through the FAQ, which
is still pretty much my first point of reference when something won't work.

Tom
Oct 13 '06 #3
But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program. That's why I can't hit on any other
way to do this.

On Oct 13, 9:58 am, "mlimber" <mlim...@gmail.comwrote:
farah727r...@gmail.com wrote:
I am trying to find the numerical value of a string that stores a two
digit number. I have found the numerical value of a char as:
char character;
cin >character;
int number = character - 48; // Computing the numberical value of
character entered
cout << "The number you entered is: " << number << endl << endl;
How do I do the same and find the numerical value of a string storing 2
digits? I know a way to do this using character arrays:
for ( ; *str != '\0' ; str++)
num = (num * 10) + (*str - 48);
But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program.
I am stumped. Any ideas?See this FAQ:

http://www.parashift.com/c++-faq-lit...sues.html#faq-...

Cheers! --M
Oct 13 '06 #4
fa**********@gmail.com wrote:
Hi all,

I am trying to find the numerical value of a string that stores a two
digit number. I have found the numerical value of a char as:

char character;
cin >character;
int number = character - 48; // Computing the numberical value of
character entered
The result of this is implementation-defined. A portable version would be:

int number = character - '0';

You should also add some error handling.
cout << "The number you entered is: " << number << endl << endl;

How do I do the same and find the numerical value of a string storing 2
digits? I know a way to do this using character arrays:
for ( ; *str != '\0' ; str++)
num = (num * 10) + (*str - 48);

But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program.

I am stumped. Any ideas?
I have no idea what "apstrings" are, but you could use a stringstream, like:

char character;
std::stringstream stream;
std::cin >character;
stream << character;
std::cin >character;
stream << character;
int number;
stream >number;

Oct 13 '06 #5
fa**********@gmail.com wrote:

But, I am not supposed to use if statements,

character arrays,
apstrings, or atoi in this program. That's why I can't hit on any other
way to do this.
apstrings? Is this the thing that was written a few years ago for the
AP comp sci test? Not part of standard C++.
>
On Oct 13, 9:58 am, "mlimber" <mlim...@gmail.comwrote:
>>farah727r...@gmail.com wrote:
>>>I am trying to find the numerical value of a
>>>>string
Let's consider the word string. If in fact you're supposed to do this,
then perhaps you are supposed to use the std::string class? If not,
then perhaps the problem is misleadingly worded?

Have you looked into std::istringstream?

>>>>that stores a two
digit number. I have found the numerical value of a char as:
>>>char character;
cin >character;
int number = character - 48; // Computing the numberical value of
Might
const int number = character - '0';
be better?

Shouldn't you check to see if the number is a digit before you do that?

>>>character entered
cout << "The number you entered is: " << number << endl << endl;
>>>How do I do the same and find the numerical value of a string storing 2
digits? I know a way to do this using character arrays:
for ( ; *str != '\0' ; str++)
num = (num * 10) + (*str - 48);
Where I assume that you had something like:
char str[BIGENOUGH];

How did you plan on putting decimal digits into str?
>>>But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program.

Why not?

LR

Oct 13 '06 #6
>
I have no idea what "apstrings" are, but you could use a stringstream, like:
apstrings were part of some classes that the Computer Science AP test
used up until they switched the programming tasks to Java around 2003.
They ap* classes were horrendous and not intended to be used other
than for teaching purposes for the test.
Oct 13 '06 #7
On 13 Oct 2006 07:42:44 -0700 in comp.lang.c++, "fa**********@gmail.com" <fa**********@gmail.comwrote,
>But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program. That's why I can't hit on any other
way to do this.
Do you have a list of things you _are_ supposed to use, or has your instructor got his head [censored]? How about strtol(), sscanf()
or std::istringstream?

int a(char *c, int v) {
return *c?a(c+1,v*10+(*c-'0')):v;
}

int main(int ac, char** av)
{
for(int ax=1; ax<ac; ++ax)
cout << a(av[ax],0) << '\n';
}

Oct 13 '06 #8

fa**********@gmail.com wrote:
Hi all,

I am trying to find the numerical value of a string that stores a two
digit number. I have found the numerical value of a char as:

char character;
cin >character;
int number = character - 48; // Computing the numberical value of
character entered
cout << "The number you entered is: " << number << endl << endl;

How do I do the same and find the numerical value of a string storing 2
digits? I know a way to do this using character arrays:
for ( ; *str != '\0' ; str++)
num = (num * 10) + (*str - 48);

But, I am not supposed to use if statements, character arrays,
apstrings, or atoi in this program.
I think you can use the boost::lexical_cast

std::string ch ;
cin >ch ;
int number = boost::lexical_cast<int>(ch) ;

It is just simple !

Oct 14 '06 #9

### This discussion thread is closed

Replies have been disabled for this discussion.

### Similar topics

 1 post views Thread by dont bother | last post: by 6 posts views Thread by mplogue | last post: by 5 posts views Thread by Marc Scheuner [MVP ADSI] | last post: by 16 posts views Thread by Martin Jørgensen | last post: by 2 posts views Thread by Ian Eagland | last post: by 2 posts views Thread by jediknight | last post: by 2 posts views Thread by farah727rash | last post: by 14 posts views Thread by Aman JIANG | last post: by 2 posts views Thread by IanSD81 | last post: by reply views Thread by AlexandraMT | last post: by 1 post views Thread by MarkDoronin | last post: by reply views Thread by milkinvl | last post: by reply views Thread by jackhack | last post: by reply views Thread by shivajikobardan | last post: by reply views Thread by gglobus | last post: by reply views Thread by Ravipg | last post: by reply views Thread by DJRhino1175 | last post: by reply views Thread by zachatmarco | last post: by