Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 27th, 2005, 1:47 PM   #1
STLFBr1
Newbie
 
Join Date: Apr 2005
Posts: 15
Rep Power: 0 STLFBr1 is on a distinguished road
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
STLFBr1 is offline   Reply With Quote
Old Apr 27th, 2005, 3:04 PM   #2
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
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;}
iignotus is offline   Reply With Quote
Old Apr 27th, 2005, 3:41 PM   #3
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Apr 27th, 2005, 3:42 PM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>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' );
Eggbert is offline   Reply With Quote
Old Apr 28th, 2005, 4:37 PM   #5
Josef_Stalin
Programmer
 
Join Date: Apr 2005
Posts: 77
Rep Power: 4 Josef_Stalin is on a distinguished road
Wouldn't it just be easier to segregate the number input into ints and the letter inputs into strings from the beginning?
Josef_Stalin is offline   Reply With Quote
Old Apr 28th, 2005, 8:48 PM   #6
STLFBr1
Newbie
 
Join Date: Apr 2005
Posts: 15
Rep Power: 0 STLFBr1 is on a distinguished road
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?
STLFBr1 is offline   Reply With Quote
Old Apr 29th, 2005, 11:04 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
for (int i = 0; (str[i] >= '0') && (str[i] <= '9'); i++)
{
    num_array[i] = str[i] - '0';
}
That should do it.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC