PDA

View Full Version : How to convert string to int




Beckie
Apr 8, 2005, 12:12 AM
Hi all
I'm brand new to this website and I was looking for some help with this program that I have to do.

I have to find a way somehow to convert a string to an int.

I have not learned sstream yet and I don't think I am allowed to use it...so I was wondering if there is any other way.

This is what I want my program to do:
cin >> date;
//example 1/1/2005
cout << date[0] << endl;
//Then I want to take the value of date[0], which is 1 and use that value as an int.
//For example, date[0] = 1 and then use that 1 as an int value to multiply by something else or add, subtract, etc and then get an int value for my final answer.

Thanks
Take care,
Beckie



greatm31
Apr 8, 2005, 01:25 AM
This may be a bit general, as I haven't done much C lately, but I think you're looking for a particular string method which reads it into an array of chars. Then, if you can be sure the date is formatted correctly, you can refer to the nth element of the array, and it'll be the character "1". Then you can convert that to an integer easily and use it as needed.

I'm darn sure that's easy to do, but I would look at a reference manual (there are many online) for string methods that allow you to do this. Good luck!

DaveP
Apr 8, 2005, 02:58 AM
You can access a particular element within the string as mentioned above, and then use the atoi (ascii to int) function to convert. http://www.cplusplus.com/ref/cstdlib/atoi.html (Reference)

It's been a little while since I've used this and think the compiler won't complain about getting a CString instead of a char* as CString will automatically convert. Don't quote me on it, you may need to allocate a temporary variable.

Beckie
Apr 8, 2005, 04:05 AM
Thank you so much everyone for your help.
I did the program that DaveP had a link to, and I am wondering if there is a different term for atoi that I am missing. I haven't learned that yet and it's not in my book.

Thanks
Take care,
Beckie

Beckie
Apr 8, 2005, 04:55 AM
Hi again
Sorry to bother everyone again, but I still can't get it to work.

I forgot to mention that I am supposed to cin the time too.

Here is the input:
4/6/2005 13:20
This is supposed to be the output:
d = 6 //for the day
m = 4 //for the month
Y = 5 //for the year
C = 20 //for the century

The values for d, m, Y, and C are all supposed to turn out to be integers but I have to enter them as strings because I couldn't figure out any other way to do it. So I have to convert the values of each location in the string to integers. For instance, date[0] == '4' and now I want that '4' to become an int.

Thanks in advance
Take care,
Beckie

nekkets
Apr 8, 2005, 07:46 AM
try this

char k;
k=date[0]-48;
int date0=k;

basically this is to change the ASCII value of the character date[0].
not too sure though...havent been touching c++ for ages,but this method should be somewhere along the line

DaveP
Apr 8, 2005, 12:33 PM
nekkets is correct, that should work.

caveman_uk
Apr 8, 2005, 01:13 PM
If the number has more than one digit you keep a total starting at zero. You then go through the string multiplying total by 10 each time and then adding the value the character represents until you run out of characters. There's almost certainly a C function for this but I've not done much 'proper' C (only OOP derivatives) so I don't know. That's how I used to do it in Z80 and 8080 assembler anyway ;)

WebMongol
Apr 9, 2005, 04:53 PM
The simplest way to convert string to int is C-function atoi. But atoi does not differentiate between "0" and invalid input. In case you need more robust way - use strtol.

Following example shows usage of atoi:

/* to compile and run:
* gcc a.c -o xa && xa -1 -10 0 1 10 100 100 a junk
*/
#include <stdio.h>

int main(int argc, char** argv)
{
int i = 0;
for (i = 1; i < argc; ++i) {
printf("string: %s:\t%d\n", argv[i], atoi(argv[i]));
}
return 0;
}

Output:
$ gcc a.c -o xa && xa -1 -10 0 1 10 100 100 a junk
string: -1: -1
string: -10: -10
string: 0: 0
string: 1: 1
string: 10: 10
string: 100: 100
string: 100: 100
string: a: 0
string: junk: 0

good luck.

Beckie
Apr 12, 2005, 11:07 AM
Thank you everyone for your help!
I really appreciate it!

I ended up getting it to work with all of your help.

Thanks again
Take care,
Beckie

nekkets
Apr 13, 2005, 03:43 AM
welcome and take care man :)