473,626 Members | 3,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2245
fa**********@gm ail.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**********@gm ail.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...@gm ail.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**********@gm ail.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::stringstre am stream;
std::cin >character;
stream << character;
std::cin >character;
stream << character;
int number;
stream >number;

(add some error handling)

Oct 13 '06 #5
LR
fa**********@gm ail.com 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++.
>
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::istringstr eam?

>>>>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**********@g mail.com" <fa**********@g mail.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::istringstr eam?

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**********@gm ail.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2046
by: dont bother | last post by:
Hey, I have these attributes: index which is a numerical value value vector which is a numerical float value and I want to concatenate like this:
6
2913
by: mplogue | last post by:
I'm trying to build a function that reads each character in a string and converts it to an ASCII numerical value. So far, so good. However, I'm running into problems when dealing with quotation marks. Using ASC(""") dosen't work, neither does ASC('"') or just ASC("). Is there any other way to handle this? Is this the only character I'll have this problem with, so should I just error trap it? Thanks!
5
6614
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, I need to try and make sure that a given string is a valid numerical value (not just int, or int32 - it could be float, decimal, what have you). VB.NET has a neat "IsNumeric" function - too bad C# doesn't have anything similar. Or does it?? I was thinking about trying to convert the string to Int32, but as I mentioned - that alone won't be good enough, and I really don't want to attempt a dozen conversions to find out - nor do...
16
3254
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is that I'm not very experienced with pointers, pointers to pointers and the like and I got 4 compiler warnings + I don't completely understand how to build this "compact matrix" (see later).
2
1582
by: Ian Eagland | last post by:
Hi I am just starting on Visual C (2003) from a VB background. I find it easier to learn by actually programming. I have fallen over at the first hurdle. How do you extract a numerical value from a text box? I have a number of reference books but it is not mentioned because I guess it is simple and I am just missing it. Regards
2
7220
by: jediknight | last post by:
Hi, I have a listview which has columns of text and columns of numerical data. I need to be able to sort these columns into ascending/desending order whenever the user clicks on the column header. The text columns are sorted correctly but the numerical columns seem not to get sorted properly. For example I get the following result when I try to sort a numerical
2
3588
by: farah727rash | last post by:
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...
14
2158
by: Aman JIANG | last post by:
hi i need a fast way to do lots of conversion that between string and numerical value(integer, float, double...), and boost::lexical_cast is useless, because it runs for a long time, (about 60 times slower than corresponding C functions) it's too expensive for my program. is there any way( library?) to do this fast and safely, please ?
2
1216
by: IanSD81 | last post by:
Does anyone know How to go about having an item in a drop down box which shows up a string but holds a numerical value or to have a word selected in a drop down box and have it hold a value that could be used in a mathematical procedure with a txt box? How to set the results as whats selected in the drop down box as values?
0
8272
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8713
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8644
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8514
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2632
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.