Numerical value of a string

F

farah727rash

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.
 
M

mlimber

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-lite/misc-technical-issues.html#faq-39.2

Cheers! --M
 
F

farah727rash

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.
 
R

Rolf Magnus

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;

(add some error handling)
 
L

LR

(e-mail address removed) wrote:

Please don't top post.
But, I am not supposed to use if statements,

What about while or for?

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++.
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?


Might
const int number = character - '0';
be better?

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


Where I assume that you had something like:
char str[BIGENOUGH];

How did you plan on putting decimal digits into str?




Why not?

LR
 
R

Ron Natalie

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.
 
D

David Harmon

On 13 Oct 2006 07:42:44 -0700 in comp.lang.c++ said:
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';
}
 
B

Bo Yang

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 !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top