![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 15
Rep Power: 0
![]() |
String to int
I'm working on a program that reads in data from a file. I have it read in the data as a string, since the one column of data is an int and the other is a int followed by several words.
Now the first thing that would help out a lot would be if I could simply read in the first number, then read in just the other number instead of having to read in the whole thing (note: there is a tab between the two and the second one is in this type of format: 11_read_and_write (0)) If I could read them in as an array of ints that would solve all my problems but if not I need to later do a comparison with the strings, both comparing the string to an int and then adding 1 to that string that stores an int. Sorry if this is confusing but heres what I'm doing, hopefully it'll help: if(store[x][1] == assign[x][0])
{
if(store[x][0] == 0)
assign[x][1]=assign[x][1]+1;
else
assign[x][2]=assign[x][2]+1;
}Also I've tried atoi but it seems that it can't do it with strings only with char. Thanks a bunch |
|
|
|
|
|
#2 |
|
Professional Programmer
|
Maybe stoi or string.c_string atoi? I'm not sure.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;} |
|
|
|
|
|
#3 |
|
Programming Guru
![]() ![]() ![]() |
fairly sure the atoi() would work.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>I have it read in the data as a string
Why? If the format is <int>\t<int><non-int>, you can still take both integers without using a string, then take the rest of the line as a string or simply discard it: int a[2]; cin>> a[0] >> a[1]; cin.ignore ( numeric_limits<streamsize>::max(), '\n' ); |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Apr 2005
Posts: 77
Rep Power: 4
![]() |
Wouldn't it just be easier to segregate the number input into ints and the letter inputs into strings from the beginning?
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2005
Posts: 15
Rep Power: 0
![]() |
Well see thats what I was trying to do but the second set of info is all attached like so: 11_read_and_write. So I don't know how to just read that number and discard the rest since I don't need it. I tried setting up an array of ints and read until it hits the underscore but that gave me errors for referencing and underscore with an int. Any ideas?
|
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
for (int i = 0; (str[i] >= '0') && (str[i] <= '9'); i++)
{
num_array[i] = str[i] - '0';
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|