Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Beckie

macrumors newbie
Original poster
Apr 6, 2005
12
0
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

macrumors member
Oct 19, 2001
69
29
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

macrumors 6502a
Mar 18, 2005
506
433
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

macrumors newbie
Original poster
Apr 6, 2005
12
0
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

macrumors newbie
Original poster
Apr 6, 2005
12
0
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

macrumors newbie
Apr 20, 2004
27
0
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
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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

macrumors member
Sep 19, 2004
50
0
Bay Area, CA
atoi and strtol

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, atoi(argv));
}
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

macrumors newbie
Original poster
Apr 6, 2005
12
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.