473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Numerical value of 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 >= '0' && *str <='9'; str++)
num = (num * 10) + (*str - '0');

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
2 3573
Suomi
2
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 >= '0' && *str <='9'; str++)
num = (num * 10) + (*str - '0');

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

I am stumped. Any ideas?

Thanks,
Farah.
Here is a sugestion:

std::string s ="48";
int n;

std::stringstream ss(s);
ss >> n;
Oct 13 '06 #2
D_C
293 100+
Expand|Select|Wrap|Line Numbers
  1. result = 0;
  2. cin >> ch;
  3. while(character is a digit)
  4. {
  5.   result *= 10;
  6.   result += ch - '0';
  7.  
  8.   cin >> ch;
  9. }
Oct 13 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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
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...
5
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 -...
16
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...
2
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...
2
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...
8
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 =...
14
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.